每当用户输入单词时,如何显示祝酒词?

时间:2013-03-27 18:46:52

标签: android

我有一个带有EditText和Button的简单布局。每当用户在EditText中输入特定单词时,我都希望显示一个Toast。我怎么能这样做?

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();
    }

}

}

3 个答案:

答案 0 :(得分:1)

TextWatcher添加EditText

本教程向您展示了您需要执行的操作:http://karanbalkar.com/2012/10/tutorial-11-using-textwatcher-in-android/

答案 1 :(得分: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();
        }
    }
}

答案 2 :(得分:0)

   EditText e = (EditText)findViewById(..); 
   e.addTextChangedListener(new TextWatcher(){

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

   });