当前位置:  开发笔记 > 编程语言 > 正文

如何更改底部导航按钮上的活动点击?

如何解决《如何更改底部导航按钮上的活动点击?》经验,为你挑选了1个好方法。

我想在我现有的Android应用程序中使用底部导航栏,但问题是所有屏幕都是活动,是否可以加载活动而不隐藏底部导航栏.

示例: activity_main.xml




    

        

    
    


这是我的基础活动,

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        BottomBar bottomBar;
        bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                if (tabId == R.id.matching) {
                    Log.i("matching","matching inside "+tabId);
                    Intent in=new Intent(getBaseContext(),Main2Activity.class);
                    startActivity(in);
                }else if (tabId == R.id.watchlist) {
                    Log.i("matching","watchlist inside "+tabId);
                    Intent in=new Intent(getBaseContext(),Main3Activity.class);
                    startActivity(in);
                }
            }
        });
    }
}

Main2Activity

public class Main2Activity extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main2);
        NestedScrollView dynamicContent = (NestedScrollView) findViewById(R.id.myScrollingContent);
        View wizard = getLayoutInflater().inflate(R.layout.activity_main2, null);
        dynamicContent.addView(wizard);

Main3Activity

public class Main3Activity extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main3);
        NestedScrollView dynamicContent = (NestedScrollView) findViewById(R.id.myScrollingContent);
        View wizard = getLayoutInflater().inflate(R.layout.activity_main3, null);
        dynamicContent.addView(wizard);


    }
}

表现




    
        
            
                

                
            
        
        
        
    


Sushil Dubey.. 16

我通过以下方式解决了这个问题:

1.使用底部导航栏创建一个BaseActivity.

 package com.example.apple.bottomnavbarwithactivity;

 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.widget.RadioButton;
 import android.widget.RadioGroup;

public class BaseActivity extends AppCompatActivity {


RadioGroup radioGroup1;
RadioButton deals;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);


    radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
    deals = (RadioButton)findViewById(R.id.deals);
    radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            Intent in;
            Log.i("matching", "matching inside1 bro" + checkedId);
            switch (checkedId)
            {
                case R.id.matching:
                    Log.i("matching", "matching inside1 matching" +  checkedId);
                    in=new Intent(getBaseContext(),MatchingActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                case R.id.watchList:
                    Log.i("matching", "matching inside1 watchlistAdapter" + checkedId);

                    in = new Intent(getBaseContext(), WatchlistActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);

                    break;
                case R.id.rates:
                    Log.i("matching", "matching inside1 rate" + checkedId);

                    in = new Intent(getBaseContext(),RatesActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                case R.id.listing:
                    Log.i("matching", "matching inside1 listing" + checkedId);
                    in = new Intent(getBaseContext(), ListingActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                case R.id.deals:
                    Log.i("matching", "matching inside1 deals" + checkedId);
                    in = new Intent(getBaseContext(), DealsActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                default:
                    break;
            }
        }
    });
   }
 }

BaseActivity布局名为base_activity.xml

    


        

            

            

            
            
            


        
    

    
    

2.在底部导航单击中要打开的所有活动中扩展BaseActivity并且还需要扩展活动布局例如,我创建了五个示例活动.

i] MatchingActivity.

  package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
              //extends our custom BaseActivity
    public class MatchingActivity extends BaseActivity {
        LinearLayout dynamicContent,bottonNavBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                        //setContentView(R.layout.activity_matching);


                       //dynamically include the  current activity      layout into  baseActivity layout.now all the view of baseactivity is   accessible in current activity.
            dynamicContent = (LinearLayout)  findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_matching, null);
            dynamicContent.addView(wizard);


              //get the reference of RadioGroup.

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.matching);

                 // Change the corresponding icon and text color on nav button click.

            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_matching_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }

    }

II] WatchlistActivity

 package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;

    public class WatchlistActivity extends AppCompatActivity {
        LinearLayout dynamicContent,bottonNavBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_watchlist1);

            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_watchlist1, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.watchList);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.favourite_heart_selected, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }
    }

III] RatesActivity

  package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;

    public class RatesActivity extends BaseActivity {
        LinearLayout dynamicContent,bottonNavBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           // setContentView(R.layout.activity_rates);

            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_rates, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.rates);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_rate_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }
    }

iv] ListingActivity

 package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;

    public class ListingActivity extends BaseActivity {
        LinearLayout dynamicContent,bottonNavBar;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             //setContentView(R.layout.activity_listing);



            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_listing, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.listing);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_listing_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }
    }

v] DealsActivity

 package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;

    public class DealsActivity extends BaseActivity {

        LinearLayout dynamicContent,bottonNavBar;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_deals);
            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_deals, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.deals);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_deals_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));



        }
    }

注意:请确保正确处理后退.我没有为背压写任何代码.

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述



1> Sushil Dubey..:

我通过以下方式解决了这个问题:

1.使用底部导航栏创建一个BaseActivity.

 package com.example.apple.bottomnavbarwithactivity;

 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.widget.RadioButton;
 import android.widget.RadioGroup;

public class BaseActivity extends AppCompatActivity {


RadioGroup radioGroup1;
RadioButton deals;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);


    radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
    deals = (RadioButton)findViewById(R.id.deals);
    radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            Intent in;
            Log.i("matching", "matching inside1 bro" + checkedId);
            switch (checkedId)
            {
                case R.id.matching:
                    Log.i("matching", "matching inside1 matching" +  checkedId);
                    in=new Intent(getBaseContext(),MatchingActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                case R.id.watchList:
                    Log.i("matching", "matching inside1 watchlistAdapter" + checkedId);

                    in = new Intent(getBaseContext(), WatchlistActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);

                    break;
                case R.id.rates:
                    Log.i("matching", "matching inside1 rate" + checkedId);

                    in = new Intent(getBaseContext(),RatesActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                case R.id.listing:
                    Log.i("matching", "matching inside1 listing" + checkedId);
                    in = new Intent(getBaseContext(), ListingActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                case R.id.deals:
                    Log.i("matching", "matching inside1 deals" + checkedId);
                    in = new Intent(getBaseContext(), DealsActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                default:
                    break;
            }
        }
    });
   }
 }

BaseActivity布局名为base_activity.xml

    


        

            

            

            
            
            


        
    

    
    

2.在底部导航单击中要打开的所有活动中扩展BaseActivity并且还需要扩展活动布局例如,我创建了五个示例活动.

i] MatchingActivity.

  package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
              //extends our custom BaseActivity
    public class MatchingActivity extends BaseActivity {
        LinearLayout dynamicContent,bottonNavBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                        //setContentView(R.layout.activity_matching);


                       //dynamically include the  current activity      layout into  baseActivity layout.now all the view of baseactivity is   accessible in current activity.
            dynamicContent = (LinearLayout)  findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_matching, null);
            dynamicContent.addView(wizard);


              //get the reference of RadioGroup.

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.matching);

                 // Change the corresponding icon and text color on nav button click.

            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_matching_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }

    }

II] WatchlistActivity

 package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;

    public class WatchlistActivity extends AppCompatActivity {
        LinearLayout dynamicContent,bottonNavBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_watchlist1);

            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_watchlist1, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.watchList);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.favourite_heart_selected, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }
    }

III] RatesActivity

  package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;

    public class RatesActivity extends BaseActivity {
        LinearLayout dynamicContent,bottonNavBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           // setContentView(R.layout.activity_rates);

            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_rates, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.rates);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_rate_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }
    }

iv] ListingActivity

 package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;

    public class ListingActivity extends BaseActivity {
        LinearLayout dynamicContent,bottonNavBar;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             //setContentView(R.layout.activity_listing);



            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_listing, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.listing);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_listing_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }
    }

v] DealsActivity

 package com.example.apple.bottomnavbarwithactivity;

    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;

    public class DealsActivity extends BaseActivity {

        LinearLayout dynamicContent,bottonNavBar;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_deals);
            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_deals, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.deals);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_deals_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));



        }
    }

注意:请确保正确处理后退.我没有为背压写任何代码.

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

推荐阅读
360691894_8a5c48
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有