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

时间:2015-02-26 21:27:53

标签: android

有人可以帮我修改我的代码我试图禁用Button如果'到'和'文字'没有进入EditText字段。这一行

post_button.setEnabled(false);

给我错误

cannot resolve symbol

正如我在Dialog中设置的那样。我如何解决这个问题来实现这一目标。

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

            final Dialog dialog = new Dialog(context);

            dialog.setContentView(R.layout.write_message_layout);

            final TextView post_button = (TextView) dialog.findViewById(R.id.button_post);
            final TextView cancel_button = (TextView) dialog.findViewById(R.id.message_cancel);
            final EditText new_message_text = (EditText) dialog.findViewById(R.id.new_message_insert_text);
            final EditText new_message_to = (EditText) dialog.findViewById(R.id.new_message_insert_to);
            //final EditText
            // = (EditText) dialog.findViewById(R.id.new_message_insert_to);

            cancel_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });
                            post_button.setEnabled(!TextUtils.isEmpty(new_message_to.getText().toString()));
            post_button.setEnabled(!TextUtils.isEmpty(new_message_text.getText().toString()));


            ///
            new_message_to.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    post_button.setEnabled(count > 0);
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                @Override
                public void afterTextChanged(Editable s) {
                }
            });


            new_message_text.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s1, int start1, int before1, int count1) {
                    post_button.setEnabled(count1 > 0);
                }
                @Override
                public void beforeTextChanged (CharSequence s1, int start1, int count1, int after1) { }
                @Override
                public void afterTextChanged(Editable s1) {
                }
            });

            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();

                }
            });

            dialog.show();
        }
    });

}

2 个答案:

答案 0 :(得分:2)

您可以为EditText使用TextWatcher,并检查计数参数以启用/禁用按钮:例如

new_message_to.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
         yourButton.setEnabled(count > 0);
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
    }
});

编辑:

post_button.setEnabled(!TextUtils.isEmpty(new_message_to.getText().toString()) && !TextUtils.isEmpty(new_message_text.getText().toString()));
你在new_message_to onTextChanged检查

 post_button.setEnabled(count > 0 && !TextUtils.isEmpty(new_message_text.getText().toString()));

在new_message_text onTextChage中你检查:

 post_button.setEnabled(count > 0 && !TextUtils.isEmpty(new_message_to.getText().toString()));

答案 1 :(得分:0)

您收到该错误是因为post_button, new_message_text, andnew_message_to范围内不存在TextWatcher

由于您将它们声明为方法中的最终变量,因此只有该方法(或直接嵌套在其中的匿名内部方法)才能访问所述变量。

如果您希望它起作用,您有两种选择:

  1. 使post_button和其他变量可全局访问
  2. post_button和其他变量传递给您的TextWatcher
  3. 前者最简单,因此,您的代码将成为:

    private TextView post_button;
    private EditText new_message_text;
    private EditText new_message_to;
    
    public void showCustomDialog(){
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.write_message_layout);
    
        post_button = (TextView) dialog.findViewById(R.id.button_post);
        final TextView cancel_button = (TextView) dialog.findViewById(R.id.message_cancel);
        new_message_text = (EditText) dialog.findViewById(R.id.new_message_insert_text);
        new_message_to = (EditText) dialog.findViewById(R.id.new_message_insert_to);
    
        cancel_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
    
        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();
            }
        });
    
        new_message_text.addTextChangeListener(textWatcher);
        new_message_to.addTextChangeListener(textWatcher);
    
        dialog.show();
    
        new_message_to.setText(""); // Trigger your TextWatcher's event listener
    }
    
    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) { }
    };
    

    其他人也注意到你从未添加过TextWatcher,所以我就这样做了。