0

我有一个带有 EditText 和 Button 的简单布局。每当用户在 EditText 中输入特定单词时,我都希望出现祝酒词。我该怎么做?

Button btn;
EditText edt;
String Event;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn=(Button)findViewById(R.id.Button1);
    edt=(EditText)findViewById(R.id.editText1);
    btn.setOnClickListener(this);
    Event=new String(edt.getText().toString());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if(Event.equals("Test")){
        Toast.makeText(this, "Great!",Toast.LENGTH_LONG).show();
    }

}

}

4

3 回答 3

1

添加一个TextWatcher到您的EditText.

本教程向您展示了您需要做什么:http: //karanbalkar.com/2012/10/tutorial-11-using-textwatcher-in-android/

于 2013-03-27T19:00:34.047 回答
1

从以下位置删除new String

Event=new String(edt.getText().toString());

像这样:

Event= edt.getText().toString();

并将所有这些放在按钮的 onClick 中:

btn.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View v) 
    {
        Event = edt.getText().toString();
        if(Event.equals("Test"))
        {
           Toast.makeText(this, "Great!",Toast.LENGTH_LONG).show();
        }
    }
}
于 2013-03-27T19:00:43.820 回答
0
   EditText e = (EditText)findViewById(..); 
   e.addTextChangedListener(new TextWatcher(){

   @override
   void afterTextChanged(Editable s)
   {
        if(the word is in s.toString())
        {
         Toast...show();
        }
   }

   });
于 2013-03-27T19:02:41.347 回答