我想敬酒使用异步事件输入到texbox中的文本。按下按钮。它编译没有错误,但是当按下按钮时没有任何反应。从我在论坛上收集到的信息来看,我的上下文是错误的。有人能帮我吗?下面是代码:
mSendButton = (Button)findViewById(R.id.button_send);
mSendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView view = (TextView)findViewById(R.id.edit_text_out);
String message = view.getText().toString();
if (message == "bla") {
Toast.makeText(getApplicationContext(), "Bla was entered", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Bye ", Toast.LENGTH_SHORT).show();
}
}
});
最佳答案
尝试这个:
mSendButton = (Button)findViewById(R.id.button_send);
mSendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView view = (TextView)findViewById(R.id.edit_text_out);
String message = view.getText().toString();
if (message == "bla") {
Toast.makeText(getActivity(), "Bla was entered", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getActivity(), "Bye ", Toast.LENGTH_SHORT).show();
}
}
});
要么:
mSendButton = (Button)findViewById(R.id.button_send);
mSendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView view = (TextView)findViewById(R.id.edit_text_out);
String message = view.getText().toString();
if (message == "bla") {
Toast.makeText(getContext(), "Bla was entered", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getContext(), "Bye ", Toast.LENGTH_SHORT).show();
}
}
});