gpt4 book ai didi

android - getApplicationContext() 不适用于异步事件

转载 作者:行者123 更新时间:2023-11-29 22:34:32 26 4
gpt4 key购买 nike

我想使用异步事件对输入到 texbox 中的文本进行 toast ,例如。按下按钮。它编译没有错误,但按下按钮时没有任何反应。从我在论坛上收集到的信息来看,我的背景是错误的。有人能帮我吗?下面是代码:

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

关于android - getApplicationContext() 不适用于异步事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2377755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com