如果EditText为空,如何禁用Button? [英] How to disable Button if EditText is empty ?

查看:75
本文介绍了如果EditText为空,如何禁用Button?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个EditText和一个Button.

I have an EditText and a Button in my application.

单击按钮时,在EditText中输入的文本将添加到ListView.

When the button is clicked,the text entered in the EditText is added to a ListView.

如果EditText为空,我想禁用Button.如何执行此操作?

I want to disable the Button if the EditText is empty.How to do this ?

这是我的按钮点击代码

ImageButton imb=(ImageButton)findViewById(R.id.btn_send);
            imb.setOnClickListener(new OnClickListener()
            {
             @Override
             public void onClick(View arg0) 
             { 
                 EditText et = (EditText)findViewById(R.id.EditText1);

                  String str = et.getText().toString();
                  web1.add(str);
                  Toast.makeText(ShoutSingleProgram.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
                  adapter1.notifyDataSetChanged();
                  et.setText("");

                    }
            });
            }

我该怎么做?

推荐答案

    editText1.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

           if(s.toString().trim().length()==0){
                button.setEnabled(false);
              } else {
                button.setEnabled(true);
              }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

这篇关于如果EditText为空,如何禁用Button?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆