将EditText值与String进行比较

时间:2014-06-07 08:43:10

标签: android android-edittext

我只是想比较EditText的值,并希望根据案例显示Toast,在我的程序中我只想要如果edittext值等于abc或ABC然后显示在Toast中完成其他需要显示错误,并为此我有写了几行简单的代码,但不知道为什么它总是显示错误,即使我输入了正确的值(即: - ABC或abc)

public class MainActivity extends ActionBarActivity {

EditText editTextInput;
String strInput;
Button btnInput;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextInput = (EditText) findViewById(R.id.editInput);
        strInput = editTextInput.getText().toString();

        btnInput = (Button) findViewById(R.id.button1);
        btnInput.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                if(strInput.equalsIgnoreCase("ABC"))
                {
                    Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();         
                }
            }
        });
    }

5 个答案:

答案 0 :(得分:1)

试试这个。

EditText内获取ClickListener 文字,如下所示

btnInput.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            strInput = editTextInput.getText().toString();

修改

将其初始化为空,如下所示

String strInput = "";

Button点击添加如下附加

strInput = strInput+","+editTextInput.getText().toString();

并将 strInput 设置为TextView,如下所示

textViewResult.setText(strInput);

编辑1:

btnInput.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View arg0) {
             String check = editTextInput.getText().toString();

              if(check.equalsIgnoreCase("ABC"))
              {
                        strInput = strInput+","+check;
                        Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
                        textViewResult.setText(strInput);
                        editTextInput.setText("");
              }
              else {
                        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();                    
              }
       }
});

答案 1 :(得分:1)

只需在EditText内获取ClickListener的值。

尝试如下 -

btnInput.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        strInput = editTextInput.getText().toString();
        if(strInput.equals("ABC")||strInput.equals("abc"))
            Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
         else 
            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();         
        }
    });

答案 2 :(得分:0)

if (editTextInput.getText().toString().equals("ABC"))
{
//...
}

答案 3 :(得分:0)

在编辑文本中添加文本时检查

  

EditText input = new EditText(context);

     

input.addTextChangedListener(new TextWatcher()   {
  @覆盖
        public void afterTextChanged(Editable s){
              如果((input.getText()。的toString())。等于(比较器))                   {
                  showToast();
                   }   }
   @覆盖
  public void onTextChanged(CharSequence s,int st,int b,int c){

       }  
     

public void beforeTextChanged(CharSequence s,int st,int c,int a){

                  }
              });

答案 4 :(得分:0)

使用equalsIgnoreCase方法,将根据@ priyanka的答案减少一个OR条件。

 btnInput.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            strInput = editTextInput.getText().toString();
            if(strInput.equalsIgnoreCase("ABC"))
                Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
             else 
                Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();         
            }
        });