如果自定义对话框中的textfield为空,则禁用按钮

时间:2015-02-26 17:26:09

标签: android

您好我在点击new_post_message时有一个自定义对话框我想让它检查之前在edittext中有什么内容,如果没有输入任何内容来禁用该按钮任何帮助都会很棒。谢谢!

post_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    items.add(new MessageItem(55, new_message_to.getText().toString(), "image", DateTime.now(), new_message_text.getText().toString()));

                    itemAdapter.notifyDataSetChanged();

                    if (v.getId() == R.id.button_post);
                    new_message_to.setText("");
                    new_message_text.setText("");

                    dialog.dismiss();
                }
                private TextWatcher textWatcher = new TextWatcher()

                {@Override
                 public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

                        new_message_to.addTextChangedListener(textWatcher);
                        new_message_text.addTextChangedListener(textWatcher);

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                        String s1 = new_message_to.getText().toString();
                        String s2 = new_message_text.getText().toString();


                        if (s1.equals("") || s2.equals("")) {
                            post_button.setEnabled(false);
                        } else {
                            post_button.setEnabled(true);
                        }
                    }
                    @Override
                    public void afterTextChanged(Editable editable) {

                    }
                };

            });

2 个答案:

答案 0 :(得分:0)

问题在于:

if (new_message_text.equals("")|| new_message_to.equals("")) {
    ....
}

new_message_text永远不等于"",因为""是EditText(用于输入文本的视图)而不是字符串。您必须将其与实际输入的文本进行比较。

所以你应该做一个:

if (new_message_text.getText().toString().equals("")|| new_message_to.getText().toString().equals("")) {
    ...
}

答案 1 :(得分:0)

您需要将textWatcher设置为editText new_message_tonew_message_text

 new_message_to.addTextChangedListener(textWatcher);
 new_message_text.addTextChangedListener(textWatcher); 

并将TextWatcher方法用作

    private TextWatcher textWatcher = new TextWatcher() {@Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                String s1 = new_message_to.getText().toString();
                String s2 = new_message_text.getText().toString();

                if (s1.equals("") || s2.equals("")) {
                    post_button.setEnabled(false);
                } else {
                    post_button.setEnabled(true);
                }
            }
        @Override
        public void afterTextChanged(Editable editable) {}
    };

并删除

 if (v.getId() == R.id.button_post);
                        new_message_to.setText("");
                        new_message_text.setText("");
从onClick方法

修改 在您的活动类中使用textWatcher方法,然后从post_button.setOnClickListener中移除并使用

new_message_to.addTextChangedListener(textWatcher);
 new_message_text.addTextChangedListener(textWatcher); 

在最后的OnCreate方法中。