用什么来代替KeyEvent?
基本上,我希望在活动启动时立即加载旋转轮,然后在活动完成时消失。但是,在我当前的代码中,它仅在我按下某个键时启动。我应该如何替换 onKeyDown 方法以便一切自动启动?
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
pd = ProgressDialog.show(this,"Working", "Fetching Grades", true,
false);
Thread thread = new Thread(this);
thread.start();
return super.onKeyDown(keyCode, event);
}
public void run(){
//String userthat = userThing.getText().toString();
//String passthis = passThing.getText().toString();
thisthing = new ClientLoginForm(user2s,pas2s);
try{
grades +=thisthing.returnGradies();
} catch (Exception e){
grades +="Connection Problems";
}
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
pd.dismiss();
tv.setText(grades + "\n " + pas2s + " " + user2s);
}
};
Basically, I want to have the spinning wheel load up immediately when the activity starts up, and then disappear when it is finished. However, in my current code, it only starts when I press a key. How should I replace the onKeyDown method so that everything starts up automatically?
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
pd = ProgressDialog.show(this,"Working", "Fetching Grades", true,
false);
Thread thread = new Thread(this);
thread.start();
return super.onKeyDown(keyCode, event);
}
public void run(){
//String userthat = userThing.getText().toString();
//String passthis = passThing.getText().toString();
thisthing = new ClientLoginForm(user2s,pas2s);
try{
grades +=thisthing.returnGradies();
} catch (Exception e){
grades +="Connection Problems";
}
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
pd.dismiss();
tv.setText(grades + "\n " + pas2s + " " + user2s);
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 onkeyDown() 代码放入活动 onResume() 它将帮助您解决问题。
put onkeyDown() code into activities onResume() It will help you to solve problem.
你能具体说明你想要它做什么吗?
您希望在加载应用程序时显示进度对话框,还是希望在获取某些内容时显示进度对话框?
-=更新=-
好的,正如我所见,您正在尝试在代码中做不同的事情。
如果您希望它在启动活动时立即显示对话框,那么您需要将 onKeyDown 中的代码也放入您的 onResume 中,如上所述。
但是,如果您尝试创建一个在选择某些内容时出现的对话框。例如,列表中的某个人,那么您就做得正确。
最后,如果您同时执行这两项操作,那么我建议您创建一个方法来制作对话框等,然后从 onKeyDown 和 onResume 中调用它
Could you specify what it is that you want it to do?
Do you want a progress dialog to show while you load your app or do you want it to show while it fetches something?
-=UPDATE=-
Okay, as I see it, you are trying to do different things in your code.
If you want it show the dialog as soon as you start the activity, then you need to put the code from onKeyDown in too your onResume, as mentioned above.
But if you are trying to make a dialog that appears when you select something. e.g. a person from a list, then you are doing it correctly.
And last, if you are doing both, then i would suggest you making a method for making the dialog and so on, and then calling this from both the onKeyDown, and the onResume