【问题标题】:Enable Action MenuItem when EditText is not empty当 EditText 不为空时启用 Action MenuItem
【发布时间】:2013-11-06 19:22:18
【问题描述】:

我想要完成的是当我的 EditText 为空时,应禁用 Action Menu 中的 MenuItem。我在 onCreateOptionsMenu 方法中为 Menu 充气,然后在 onPrepareOptionsMenu 方法中禁用 Send 按钮并保存对 Menu 的引用,以便我可以在 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;
}

这段代码工作正常,并且完全按照它应该做的事情直到屏幕方向改变。我知道当这种情况发生时,活动将被销毁并再次创建。问题是,当重新创建活动时,调用 onTextChangedbeforeTextChangedafterTextChanged 方法,就好像用户更改了文本(他没有)。 而且,这三个方法都是在onCreateOptionsMenuonPrepareOptionsMenu方法被调用之前调用的,这基本上意味着在 onTextChanged 中,我正在尝试访问当时甚至不存在的菜单对象。这当然会导致空指针异常和应用程序崩溃。

为了说明这种行为,下面是改变屏幕方向时 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

所以我的问题是,我在这里遗漏了什么吗?对于我要完成的工作,还有其他更好的方法吗?或者有什么方法可以让 Android 在屏幕方向改变时调用 onTextChanged?

【问题讨论】:

    标签: java android android-actionbar menuitem screen-orientation


    【解决方案1】:

    如果您使用为横向模式定义的布局更改方向,应用程序的行为是什么? 尝试在 /res 文件夹下创建一个 layout-land 文件夹并将您的布局文件复制粘贴到该文件夹​​。

    【讨论】:

    • @Jakkub00:在开始时将布尔标志设置为 false 并仅在您膨胀菜单时将其设置为 true 怎么样。在侦听器中,检查标志,然后执行 mymenu.finditem 语句
    • @Jakkub00:你能找到解决问题的方法吗?如果不是stackoverflow.com/questions/15992598/…,请参考以下链接
    猜你喜欢
    • 2015-08-27
    • 2022-06-12
    • 2019-08-04
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    相关资源
    最近更新 更多