【问题标题】:Verify that EditText is empty and waiting User entering data验证 EditText 是否为空并等待用户输入数据
【发布时间】:2012-02-28 18:46:11
【问题描述】:

我的布局是:

1 EditText (id = etvalue1)
1 EditText (id = etvalue2)
1 Button (id = save)

我需要知道如何使用这些小部件执行以下操作:

  • 如果用户单击 EditText 为空的按钮,则会显示一条消息并等待用户输入值。
  • 显示消息直到用户输入数值后的数值是输入计算(etvalue1+etvalue2)并显示结果。

【问题讨论】:

    标签: android events android-edittext


    【解决方案1】:

    在按钮的 onclicklistener 中执行以下操作

    if(edit1.getText().equals("") || edit2.getText().equals(""))  
    {
         //toast the error message 
         return;
    }
     //calculate the result
    

    toast 的最长时间是 Toast.LENGTH_LONG。如果您希望在用户在 edittext 中输入数据之前显示消息,则使用 textview 显示消息。在两个 EditTexts 上设置 onTextChangedListener 以检查文本是否已更改。在 afterTextChanged 方法中可以进行计算。

    【讨论】:

      【解决方案2】:

      如果要检测用户何时在edittext中输入值,则需要像Counting Chars in EditText Changed Listener中那样使用事件来观察变化

      阅读reference 中的相关方法

      或者,您可以只检查按钮的点击事件:

      button.setOnClickListener(new View.OnClickListener() {
                   public void onClick(View v) {
                       if(etvalue1.getText().toString().equals(''))
                           //empty text condition
                   }
               });
      

      【讨论】:

        【解决方案3】:

        您可以使用下面的代码来确定 EditText 是否为空:

        String sEdit1 = Edit1.getText().toString();
        
        if (sEdit1.matches(""))
                {
                    Toast.makeText(getBaseContext(), "EditText is empty", Toast.LENGTH_SHORT).show();     
                    return;
                }
        

        【讨论】:

          【解决方案4】:
          if(edit1.getText().equals("") || edit1.getText().equals(null))  {
              //toast the message 
          } else {
              //calculate the result
          }
          

          【讨论】:

            【解决方案5】:

            在Activity Class的onCreate方法中写下这段代码:

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                editText1 = (EditText)findViewById(R.id.etvalue1);
                editText2 = (EditText)findViewById(R.id.etvalue2);
                button = (Button)findViewById(R.id.save);
            
                button.setOnClickListener(new OnClickListener() {
            
                    public void onClick(View v) {
                        if(!(editText1.getText().toString().equalsIgnoreCase(""))){
                            if(!(editText2.getText().toString()).equalsIgnoreCase("")){
                                addNumbers();
                            } else {
                                Toast.makeText(getApplicationContext(),
                                        "Enter no.", Toast.LENGTH_LONG).show();
                            }
                        } else {
                            Toast.makeText(getApplicationContext(),
                                    "Enter no.", Toast.LENGTH_LONG).show();
                        }
            
                    }
            
                    private void addNumbers() {
            
                    }
                });
            
            }
            

            }

            在 addNumbers 方法中,从 EditTest 中获取值,然后做任何你想做的事情。

            【讨论】:

              【解决方案6】:
              if(edit1.getText().equals("") || edit2.getText().equals(""))  
              {
                   //toast the error message 
                   Dialog dialog=new Dialog(getApplicationContext());
                   LinearLayout llView=new LinearLayout(getApplicationContext());
                   EditText editText1=new EditText(getApplicationContext());
                   EditText editText2=new EditText(getApplicationContext());
                   wsutText2.addTextChangedListener(new TextWatcher(){
                      public void afterTextChanged(Editable s) {
                          //do your calculations
                      }
                      public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                      public void onTextChanged(CharSequence s, int start, int before, int count){}
                  }); 
              
                  editText2.addTextChangedListener(new TextWatcher(){
                      public void afterTextChanged(Editable s) {
                          //do your calculations
                      }
                      public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                      public void onTextChanged(CharSequence s, int start, int before, int count){}
                  }); 
              llView.addView(editText1);
              llView.addView(editText2);
              dialog.setContentView(llView);
              dialog.show();
                   return;
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-07-20
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多