Android 如何仅在没有用户输入时禁用按钮?

Android 如何仅在没有用户输入时禁用按钮?,android,while-loop,Android,While Loop,我的目标是在编辑文本字段中获得输入。只要用户没有输入所有详细信息,就可以禁用“提交”按钮。然而,我在尝试这样做时遇到了一个问题 我对意图或屏幕没有问题 守则: public class Welcome extends Activity { EditText efn,eln,eage; Button submit; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt

我的目标是在编辑文本字段中获得输入。只要用户没有输入所有详细信息,就可以禁用“提交”按钮。然而,我在尝试这样做时遇到了一个问题

我对意图或屏幕没有问题

守则:

public class Welcome extends Activity  
{

EditText efn,eln,eage;
Button submit;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);




    efn=(EditText)findViewById(R.id.fname);
    eln=(EditText)findViewById(R.id.lname);
    eage=(EditText)findViewById(R.id.age);
    submit=(Button)findViewById(R.id.submit);


    submit.setEnabled(CheckIfOkInput());

                                        // I guess the problem is over here <--
if(CheckIfOkInput()==false)
    {
        while(CheckIfOkInput())
        {
            efn=(EditText)findViewById(R.id.fname);
            eln=(EditText)findViewById(R.id.lname);
            eage=(EditText)findViewById(R.id.age);
        }

        submit.setEnabled(true);
    }



    submit.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {


                Intent iHome=new Intent (Welcome.this,Home_page.class);
                String ln=eln.getText().toString();
                String fn=efn.getText().toString();
                int age=Integer.valueOf (eage.getText().toString());

                iHome.putExtra("fname", fn);
                iHome.putExtra("lname", ln);
                iHome.putExtra("age", age);

                startActivity(iHome);



        }
    });


}

     public boolean CheckIfOkInput()
{

    if(this.eln.getText().toString()==""||this.efn.getText().toString()=="")
    {
        return false;
    }


    return true;

}

 }
公共类欢迎扩展活动
{
编辑文本efn、eln、eage;
按钮提交;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
efn=(EditText)findViewById(R.id.fname);
eln=(EditText)findViewById(R.id.lname);
eage=(EditText)findViewById(R.id.age);
提交=(按钮)findViewById(R.id.submit);
submit.setEnabled(CheckIfOkInput());

//我想问题就在这里暂时不要使用,因为你将一直在“做”一些事情。你需要一个监听器。就像一个按钮的onclick监听器,但是编辑文本字段的on-change监听器一样

方法:创建一个列表,每次(对于初学者,可以稍后更新)注册更改时,检查inputfield是否为空。如果为空,则禁用按钮,否则启用它

创建changelistener很容易找到,例如:


这个问题中的代码是关于计数字符的,但这几乎正是您想要的:计数字符,以及它们是否为0(或不是0)做点什么。

您可以制作如下代码,在每个
EditText
上添加一个侦听器,当
EditText
的文本发生更改时,您可以验证两个
EditText
是否都有文本。 顺便说一下,使用
.equals
方法来比较字符串,如果
this.eln.getText().toString()返回null,则可以使用
.equals(this.eln.getText().toString())
来避免NullPointerException

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(!"".equals(this.eln.getText().toString()) && !"".equals(this.efn.getText().toString()){
            submit.setEnabled(true);
        }
    }
}

我对所有这些编程都是新手。所以我不太了解你。你介意更具体一点吗?比如-EditTextListener做什么?它“侦听”更改。这就是侦听器所做的。onclick侦听器等待(侦听)你点击,当你点击时,它“激发”。onchangelistener等待直到有更改(例如userinput)然后开火。你不需要不断地检查,你可以“等待”(听)但是EditTextLisnter有什么好处?我的意思是-它做什么?它是一个侦听器,上面的方法,onTextChanged,在每次edittext的文本更改时都会被调用。因此,每次用户在edittext上写东西时,它都会调用onTextChanged并进行验证如果两个EditText都有一些文本。好吧,我现在明白了,按钮确实只有在有文本时才禁用。但由于某种原因,我按下按钮后按钮不工作。并使应用程序停止工作。你能发布错误日志吗?要对按钮执行某些操作,你需要使用submit.setOnClickListener方法,并在override onc中单击方法,你可以设置按钮的动作。啊,我找到了。我只是没有在第三个编辑文本上设置listner。