onNavigationItemSelected在操作栏上设置活动项位置标题

时间:2015-03-20 07:54:59

标签: android

动作条

让我试着解释一下,我有一个带有可选项的下拉动作栏,主页,联系我们和关于。

  1. 加载主活动时,ActionBar下拉列表中显示的默认项目为Home。行。
  2. 当我选择第二项(联系我们)时,CURRENT ACTIVITY(主要活动)将自己设为“联系我们”,然后......
  3. ..然后启动NEXT ACTIVITY(联系我们),但将ActionBar设置为默认“Home”而不是“contact us”
  4. 请参阅下面的代码:

    public class MainActivity extends Activity {
    
    private boolean mNaviFirstHit = true;
    ….
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    SpinnerAdapter sadapter = ArrayAdapter.createFromResource(this, R.array.actions, R.layout.dropdown_textcolor);
    
        // Callback
        OnNavigationListener callback = new OnNavigationListener() {
    
            String[] items = getResources().getStringArray(R.array.actions)
            @Override
            public boolean onNavigationItemSelected(int position, long id) {
    
                  if (mNaviFirstHit) {
                        mNaviFirstHit = false;
                        return true;
                    }
    
                if (items[position].equals("Home"))
                {
                    System.out.println("Selected Home");
                    startActivity(new Intent(getApplicationContext(),MainActivity.class));
                }
                if (items[position].equals("Contact Us"))
                {
                    System.out.println("Selected Contact US");
                    startActivity(new Intent(getApplicationContext(),ActionContact.class));
                }
                if (items[position].equals("About"))
                {
                    System.out.println("Selected About");
                    startActivity(new Intent(getApplicationContext(),ActionAbout.class));  
                }
    

0 个答案:

没有答案