开启/关闭动作条菜单项 [英] Enable/Disable ActionBar Menu Item

查看:123
本文介绍了开启/关闭动作条菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有动作条菜单项取消和保存。

menu.xml文件

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:ID =@ + ID / saveButton
          机器人:showAsAction =总是
          机器人:标题=@字符串/保存
          机器人:可见=真>

    < /项目>
    <项目机器人:ID =@ + ID / cancelButton
          机器人:showAsAction =总是
          机器人:标题=@字符串/取消
          机器人:可见=真>
    < /项目>

< /菜单>
 

我想禁用保存菜单项时,活动开始了。

我的活动code -

  @覆盖
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.add_project);

        EditText上PROJECTNAME =(EditText上)findViewById(R.id.editTextProjectName);
        projectName.addTextChangedListener(新TextWatcher(){

            @覆盖
            公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
                configureSaveButton(多个);
            }
            @覆盖
            公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){}
            @覆盖
            公共无效afterTextChanged(编辑S){}
        });
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        MenuInflater充气= getMenuInflater();
        inflater.inflate(R.menu.addprojectme​​nu,菜单);
        返回super.onCreateOptionsMenu(菜单);
    }

    @覆盖
    公共布尔prepareOptionsMenu(功能菜单)在{
        菜单项项=(菜单项)findViewById(R.id.saveButton);
        item.setEnabled(假);
        返回super.on prepareOptionsMenu(菜单);
    }

    私人无效configureSaveButton(CharSequence中){
        字符串文本= NULL;
        如果(S!= NULL){
            文= s.toString();
        }
        如果(文= NULL和放大器;!&安培; text.trim()长度()!= 0){

        }其他{

        }
    }
 

那么,我想在这里做的是,首先,当活动开始的保存菜单项应该被禁用时editext包含一些文本,那么它应该被启用。

我不知道什么应该是code中的if else在configureSaveButton方法。 另外我如何可以禁用保存菜单项开始。

我得到空指针异常的prepareOptionsMenu。

我采用了android 4.1

解决方案

  @覆盖
公共布尔prepareOptionsMenu(功能菜单)在{

    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.addprojectme​​nu,菜单);

    menu.getItem(0).setEnabled(假); //这里通过保存菜单项的索引
    返回super.on prepareOptionsMenu(菜单);

}
 

只是夸大它在prepare时间和膨胀的菜单中没有必要膨胀 oncreateoptionemenu 时间后停用或者你可以用最后两行code从 onCreateOptionMenu 充气后。

  @覆盖
公共布尔prepareOptionsMenu(功能菜单)在{

    menu.getItem(0).setEnabled(假); //这里通过保存菜单项的索引
    返回super.on prepareOptionsMenu(菜单);

}
 

I have actionbar menuitems cancel and save.

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/saveButton" 
          android:showAsAction="always"          
          android:title="@string/save" 
          android:visible="true">

    </item>
    <item android:id="@+id/cancelButton" 
          android:showAsAction="always"         
          android:title="@string/cancel" 
          android:visible="true">        
    </item>

</menu>

I want to disable save menuitem when activity is started.

My activity code -

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_project);

        EditText projectName = (EditText) findViewById(R.id.editTextProjectName);   
        projectName.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                configureSaveButton(s);             
            }           
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            @Override
            public void afterTextChanged(Editable s) {}
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.addprojectmenu, menu);      
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = (MenuItem) findViewById(R.id.saveButton);
        item.setEnabled(false);     
        return super.onPrepareOptionsMenu(menu);
    }

    private void configureSaveButton(CharSequence s){
        String text = null;
        if(s != null){
            text = s.toString();
        }       
        if(text != null && text.trim().length() != 0){

        }else{

        }
    }

So what I am trying to do here is, initially when activity is started save menu item should be disabled and when editext contains some text then it should be enabled.

I am not sure what should be the code in if else in configureSaveButton method. Also how can i disable save menu item initially.

I get null pointer exception in onPrepareOptionsMenu.

I am using android 4.1

解决方案

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.addprojectmenu, menu);      

    menu.getItem(0).setEnabled(false); // here pass the index of save menu item
    return super.onPrepareOptionsMenu(menu);

}

Just inflate it on prepare time and disable after inflated menu no need to inflate in oncreateoptionemenu time or you can just use last two line of code after inflating from onCreateOptionMenu.

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    menu.getItem(0).setEnabled(false); // here pass the index of save menu item
    return super.onPrepareOptionsMenu(menu);

}

这篇关于开启/关闭动作条菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆