Android移动开发-选项菜单和子菜单(SubMenu)的实现

package com.fukaimei.menutest;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText edit;

    // 定义“字体大小”菜单项的标识
    final int FONT_10 = 0x111;
    final int FONT_12 = 0x112;
    final int FONT_14 = 0x113;
    final int FONT_16 = 0x114;
    final int FONT_18 = 0x115;
    // 定义“普通菜单项”的标识
    final int PLAIN_ITEM = 0x11b;
    // 定义“字体颜色”菜单项的标识
    final int FONT_RED = 0x116;
    final int FONT_BLUE = 0x117;
    final int FONT_GREEN = 0x118;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit = (EditText) findViewById(R.id.txt);
    }
    // 当用户单击MENU键时触发该方法

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 向menu中添加“字体大小”的子菜单
        SubMenu fontMenu = menu.addSubMenu("字体大小");
        // 设置菜单的图标
        fontMenu.setIcon(R.mipmap.ic_launcher_round);
        // 设置菜单头的图标
        fontMenu.setHeaderIcon(R.mipmap.ic_launcher_round);
        // 设置菜单头的标题
        fontMenu.setHeaderTitle("选择字体大小");
        fontMenu.add(0, FONT_10, 0, "10号字体");
        fontMenu.add(0, FONT_12, 0, "12号字体");
        fontMenu.add(0, FONT_14, 0, "14号字体");
        fontMenu.add(0, FONT_16, 0, "16号字体");
        fontMenu.add(0, FONT_18, 0, "18号字体");
        // 向menu中添加“普通菜单项”
        menu.add(0, PLAIN_ITEM, 0, "普通菜单项");
        // 向menu中添加“字体颜色”的子菜单
        SubMenu colorMenu = menu.addSubMenu("字体颜色");
        colorMenu.setIcon(R.mipmap.ic_launcher_round);
        // 设置菜单头的图标
        colorMenu.setHeaderIcon(R.mipmap.ic_launcher_round);
        // 设置菜单头的标题
        colorMenu.setHeaderTitle("选择文字颜色");
        colorMenu.add(0, FONT_RED, 0, "红色");
        colorMenu.add(0, FONT_GREEN, 0, "绿色");
        colorMenu.add(0, FONT_BLUE, 0, "蓝色");
        return super.onCreateOptionsMenu(menu);
    }

    // 选项菜单的菜单项被单击后的回调方法
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 判断单击的是哪个菜单项,并有针对性地做出响应
        if (item.getItemId() == FONT_10) {
            edit.setTextSize(10 * 2);
        } else if (item.getItemId() == FONT_12) {
            edit.setTextSize(12 * 2);
        } else if (item.getItemId() == FONT_14) {
            edit.setTextSize(14 * 2);
        } else if (item.getItemId() == FONT_16) {
            edit.setTextSize(16* 2);
        } else if (item.getItemId() == FONT_18) {
            edit.setTextSize(18 * 2);
        } else if (item.getItemId() == FONT_RED) {
            edit.setTextColor(Color.RED);
        } else if (item.getItemId() == FONT_GREEN) {
            edit.setTextColor(Color.GREEN);
        } else if (item.getItemId() == FONT_BLUE) {
            edit.setTextColor(Color.BLUE);
        } else if (item.getItemId() == PLAIN_ITEM) {
            Toast.makeText(this, "您点击了普通菜单项", Toast.LENGTH_SHORT).show();
        }
        return true;
    }

}
发布了70 篇原创文章 · 获赞 144 · 访问量 22万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

android 获取menu控件空指针异常

05-12

这是我的代码: ``` public class MainActivity extends AppCompatActivity { private static Toolbar action_add; private static Menu menu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); onPrepareOptionsMenu(menu); action_add.setOnClickListener(new Toolbar.OnClickListener() { public void onClick(View v) { showDialog_Layout(MainActivity.this); } }); } @Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); menu.findItem(R.id.action_add).setChecked(true); return true; } private void showDialog_Layout(Context context) { LayoutInflater inflater = LayoutInflater.from(this); final View textEntryView = inflater.inflate(R.layout.dialoglayout, null); final EditText edtInput=(EditText)textEntryView.findViewById(R.id.edtInput); final AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setCancelable(false); builder.setIcon(R.drawable.a); builder.setTitle("Title"); builder.setView(textEntryView); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle(edtInput.getText()); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle(""); } }); builder.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); } } ``` 这是我的错误: Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference 在 onPrepareOptionsMenu(menu); menu.findItem(R.id.action_add).setChecked(true); 两行,大神给看看呗!!! 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览