zoukankan      html  css  js  c++  java
  • 菜单项(Menu)的初步认识 以及 多级菜单(SubMenu)的初步认识

    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);
        }
    }
    

      

    ---------------------------------------------------------------------------------------------我是华丽丽de分界线-------------------------------------------------------------------------------

    SubMenu多级菜单 子菜单

    方法一:动态加载

    MainActivity.class
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            //添加子菜单
            //addSubMenu有子菜单的主菜单
            SubMenu file = menu.addSubMenu("文件");
            SubMenu edit = menu.addSubMenu("编辑");
            //嵌套菜单SubMenu
            SubMenu A = menu.addSubMenu("第一层");
            SubMenu AA =  A.addSubMenu("第二层");
            SubMenu AAA =  AA.addSubMenu("第三层");
            AAA.add(3,1,1,"第四层");
    
    
            file.add(1,1,1,"新建");
            file.add(1,2,1,"打开");
            file.add(1,3,1,"保存");
            file.setHeaderTitle("文件de操作");
    
    
            edit.add(2, 1, 1, "复制");
            edit.add(2,2,1,"粘贴");
            edit.add(2,3,1,"剪切");
            edit.setHeaderTitle("编辑de操作");
    
    
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if(item.getGroupId() == 1){
                switch (item.getItemId()){
                    case 1:
                        Toast.makeText(MainActivity.this,"点击了新建",Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this,"点击了打开",Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this,"点击了保存",Toast.LENGTH_SHORT).show();
                        break;
                }
            }else if(item.getGroupId() == 2){
                switch (item.getItemId()){
                    case 1:
                        Toast.makeText(MainActivity.this,"点击了复制",Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this,"点击了粘贴",Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this,"点击了剪切",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
    
            return super.onOptionsItemSelected(item);
        }
    }
    

      方法二、静态加载

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_main,menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            switch (item.getItemId()){
                case R.id.edit_copy:
                    Toast.makeText(MainActivity.this,"点击了copy",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.edit_jianqie:
                    Toast.makeText(MainActivity.this,"点击了剪切",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.edit_nietian:
                    Toast.makeText(MainActivity.this,"点击了粘贴",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.file_new:
                    Toast.makeText(MainActivity.this,"点击了新建",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.file_open:
                    Toast.makeText(MainActivity.this,"点击了打开",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.file_save:
                    Toast.makeText(MainActivity.this,"点击了保存",Toast.LENGTH_SHORT).show();
                    break;
            }
    
    
            return super.onOptionsItemSelected(item);
        }
    }
    

      menu_main.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
        <item
            android:id="@+id/file"
            android:title="文件"
            app:showAsAction="never" >
    
            <menu >
                <item
                    android:id="@+id/file_new"
                    app:showAsAction="never"
                    android:title="新建"
                    />
                <item
                    android:id="@+id/file_open"
                    app:showAsAction="never"
                    android:title="打开"
                    />
                <item
                    android:id="@+id/file_save"
                    app:showAsAction="never"
                    android:title="保存"
                    />
            </menu>
    
        </item>
    
        <item
            android:id="@+id/edit"
            android:title="编辑"
            app:showAsAction="never" >
    
            <menu>
                <item
                    android:id="@+id/edit_copy"
                    app:showAsAction="never"
                    android:title="复制"
                    />
                <item
                    android:id="@+id/edit_nietian"
                    app:showAsAction="never"
                    android:title="粘贴"
                    />
                <item
                    android:id="@+id/edit_jianqie"
                    app:showAsAction="never"
                    android:title="剪切"
                    />
            </menu>
    
        </item>
    
    
    </menu>
    

      

  • 相关阅读:
    线性回归——梯度下降
    Python 实现 KNN(K-近邻)算法
    Python 增加博客园阅读量
    阿里云CentOS安装配置Python3.7及pip3
    OnlineJudge难度与正确度的相关性检验
    jsp、jQuery、servlet交互实现登录功能
    Java Web中提交表单之后跳转到WebContent目录下的子目录里的jsp文件
    python3爬虫——下载unsplash美图到本地
    defer原理、性能、优化
    Python生成器和迭代器
  • 原文地址:https://www.cnblogs.com/zmaibbs7/p/4888015.html
Copyright © 2011-2022 走看看