简体   繁体   English

当EditText不为空时启用Action MenuItem

[英]Enable Action MenuItem when EditText is not empty

What I'm trying to accomplish is when my EditText is empty, the MenuItem in Action Menu should be disabled. 我要完成的工作是当我的EditText为空时,应禁用“操作”菜单中的MenuItem。 I'm inflating the Menu in onCreateOptionsMenu method, then I disable the Send button in onPrepareOptionsMenu method and save the reference to the Menu so I can use it in my TextWatcher which is set in onCreate method. 我在onCreateOptionsMenu方法中夸大Menu,然后在onPrepareOptionsMenu方法中禁用“发送”按钮并保存对菜单的引用,以便可以在onCreate方法中设置的TextWatcher中使用它。

Menu myMenu;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_task);

    // Set up TextWatcher
    EditText ed = (EditText) findViewById(R.id.new_task_title);
    ed.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.d("MYLOG", "This is onTextChanged event");

            if (s == null || s.length() == 0) {
                myMenu.findItem(R.id.action_send).setEnabled(false);
            }
            else {
                myMenu.findItem(R.id.action_send).setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            Log.d("MYLOG", "This is beforeTextChanged event");
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.d("MYLOG", "This is afterTextChanged event");
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.new_task, menu);

    Log.d("MYLOG", "This is onCreateOptionsMenu event");
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    Log.d("MYLOG", "This is onPrepareOptionsMenu event");

    // Save reference to the menu
    myMenu = menu;

    // Disable Send Button
    myMenu.findItem(R.id.action_send).setEnabled(false);

    return true;
}

This code works fine and does exactly what it should do until the screen orientation is changed. 该代码可以正常工作,并且可以完全执行更改屏幕方向之前的工作。 I understand that when this happens, the activity is destroyed and created again. 我知道发生这种情况时,该活动将被破坏并再次创建。 The problem is, that as the activity is recreated, the onTextChanged , beforeTextChanged and afterTextChanged methods are called as if user changed the text (which he didn't). 问题在于,随着活动的重新创建, onTextChangedbeforeTextChangedafterTextChanged方法将被调用,就像用户更改了文本一样(他没有这样做)。 Moreover , all of these three methods are called before the onCreateOptionsMenu and onPrepareOptionsMenu methods are called, which basically means that in onTextChanged I'm trying to access menu object that doesn't even exist at that moment. 此外 ,所有这三种方法被称为onCreateOptionsMenuonPrepareOptionsMenu方法被调用之前 ,这基本上意味着,在onTextChanged我试图进入菜单对象,甚至没有在那一刻存在。 Which, of course, results in null pointer exception and crash of the application. 当然,这会导致空指针异常并导致应用程序崩溃。

To illustrate this behavior, here's what the CatLog looks like when the screen orientation is changed. 为了说明这种现象,以下是更改屏幕方向时CatLog的外观。

11-06 11:55:39.142: D/MYLOG(32527): This is beforeTextChanged event
11-06 11:55:39.147: D/MYLOG(32527): This is onTextChanged event
11-06 11:55:39.147: D/MYLOG(32527): This is afterTextChanged event
11-06 11:55:39.207: D/MYLOG(32527): This is onCreateOptionsMenu event
11-06 11:55:39.207: D/MYLOG(32527): This is onPrepareOptionsMenu event
11-06 11:55:39.232: D/MYLOG(32527): This is onPrepareOptionsMenu event

So my question is, am I missing something here? 所以我的问题是,我在这里错过了什么吗? Is there any other, better approach to what I'm trying to accomplish? 还有其他更好的方法可以解决我要完成的工作吗? Or is there any way how I can make Android not to call onTextChanged when screen orientation changes? 还是有什么办法可以使Android在屏幕方向更改时调用onTextChanged?

What is the behaviour of the application if you change the orientation with layout defined for landscape mode? 如果使用横向模式定义的布局更改方向,则应用程序的行为是什么? Try creating a layout-land folder under /res folder and copy paste your layout file to that folder. 尝试在/ res文件夹下创建一个layout-land文件夹,然后将您的布局文件复制粘贴到该文件夹​​中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM