簡體   English   中英

啟用/禁用ActionBar菜單項

[英]Enable/Disable ActionBar Menu Item

我有動作欄menuitems取消並保存。

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>

我想在活動開始時禁用保存menuitem。

我的活動代碼 -

@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{

        }
    }

所以我在這里要做的是,最初當活動開始時,應該禁用保存菜單項,當editext包含一些文本時,應該啟用它。

我不確定configureSaveButton方法中的if中的代碼應該是什么。 另外我如何最初禁用保存菜單項。

我在onPrepareOptionsMenu中得到空指針異常。

我使用的是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);

}

只需在准備時間充氣,並在充氣菜單后禁用,不需要在oncreateoptionemenu時間inflate ,或者你可以在從onCreateOptionMenu充氣后使用最后兩行代碼。

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

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

}

我發現這篇文章是因為我想達到同樣的效果。 其他答案都沒有完全有助於我的工作。 經過一些研究和反復試驗,我得到了我的工作。 所以我決定分享我的結果。

我為此任務創建的變量:

MenuItem save_btn;
boolean b = false;`

然后設置操作欄菜單:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.update_menu_item, menu);
    save_btn = (MenuItem) menu.findItem(R.id.action_save);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu){
    save_btn.setEnabled(b);
    super.onPrepareOptionsMenu(menu);       
    return true;
}

由於變量boolean b初始化為false, save_btn在創建活動時禁用save_btn

以下是使用@OpenSourceRulzz示例切換save_btn的方法:

private void updateSaveButton (CharSequence s){
    String text = null;
    if(s != null){
        text = s.toString();
    }
    if(text != null && text.trim().length() != 0){
        b = true;
    }
    else{
        b = false;
    }
}

使用@OpenSourceRulzz示例再次通過onCreate()EditText框的TextWatcher()函數調用此方法

@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) {
            updateSaveButton(s);
            invalidateOptionsMenu();
        }           
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,int after){}
        @Override
        public void afterTextChanged(Editable s) {}
    });
}

拼圖的最后一部分是添加invalidateOptionsMenu()方法。

我提出的使我的工作的部分是使用boolean b變量來切換save_btn的狀態。

在這個愚蠢的問題之后奮斗了1個小時之后,唯一對我有用的解決方案是:

將Menu存儲在本地變量中:

Menu menu;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.menu = menu;
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

然后只需調用:

menu.findItem(R.id.action_save).setEnabled(true);

你最初可以在xml中禁用它...

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (your condition) {
        menu.findItem(R.id.saveButton).setEnabled(false);
    } else {
        menu.findItem(R;id.saveButton).setEnabled(true);
    }     
    return super.onPrepareOptionsMenu(menu);
}

使用此方法,每次檢查條件時都會禁用菜單項,否則將啟用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM