gpt4 book ai didi

java - 为什么我的处理程序不工作?

转载 作者:行者123 更新时间:2023-12-01 14:23:56 25 4
gpt4 key购买 nike

当我去运行应用程序时,一切似乎都很好,但是当我按下“开始”按钮时,它不显示“Hello”,我什至尝试在线程之前设置文本,但它仍然不起作用。为什么会发生这种情况?

这是代码:

public class MainActivity extends Activity implements OnClickListener {
Handler mHandler;
Button enter;
Button start;

TextView显示;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enter = (Button)findViewById(R.id.enter);
start = (Button)findViewById(R.id.start);
display =(TextView)findViewById(R.id.Display);

mHandler = new Handler(){
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bundle bundle = msg.getData();
String string = bundle.getString("myKey");
display.setText(string);

}
};
}

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

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.enter:
break;
case R.id.start:
Thread setText = new Thread(){

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Message msg= Message.obtain();
Bundle bundle = new Bundle();
String dateString;
dateString = "Hello";
bundle.putString("myKey", dateString);
msg.setData(bundle);
mHandler.sendMessage(msg);
}



};
setText.start();
break;
}
}

}

最佳答案

您没有在 onCreate() 方法中的输入/开始按钮上注册监听器。只需调用 f.e:

enter.setOnClickListener(this);

关于java - 为什么我的处理程序不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17314374/

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