码迷,mamicode.com
首页 > 其他好文 > 详细

菜单项(Menu)的初步认识

时间:2015-10-17 20:30:21      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

 

技术分享

技术分享

MainActivity.class
public class MainActivity extends AppCompatActivity {
    private  TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.txt);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //方法一、静态添加 xml方式添加菜单
//        getMenuInflater().inflate(R.menu.menu_main, menu);
        //方法二、动态添加 代码添加菜单项
        MenuItem item = menu.add(1, 100, 1, "赵1"); //参数(groupID , ItemID , order ,title)
        item.setTitle("aaa");
        menu.add(1,101,1,"赵2");
        menu.add(1,102,1,"赵3");
        menu.add(1,5,1,"赵4");
        
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (id){
            case 100:{
                Intent intent = new Intent(MainActivity.this,second_demo.class);
                item.setIntent(intent); //跳转到其他activity
                textView.setText("点击了" + id);
                break;
            }
            case 101:{
                textView.setText("点击了" + id);
                break;
            }
            case 102:{
                textView.setText("点击了" + id);
                break;
            }
            case 5:{
                textView.setText("点击了" + id);
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }
}

  

菜单项(Menu)的初步认识

标签:

原文地址:http://www.cnblogs.com/zmaibbs7/p/4888015.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!