我试图在几秒钟后关闭ProgressDialog
(仅视觉主题),但没有成功。要关闭对话框,我希望单击Enter键,但即使在事件中完成单击,对话框也始终可见。这是我正在使用的代码:
lectura.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent keyevent) {
if(pDialog == null) {
pDialog = new ProgressDialog(ValidacionBlueTooth.this);
pDialog.setMessage("Sincronizando...");
pDialog.setCancelable(false);
pDialog.show();
}
if ((keyevent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
Intent intent = new Intent(ValidacionBlueTooth.this, Validador.class);
startActivity(intent);
}
修改
我收到此消息,阻止对话关闭,并继续运行代码。
01-27 16:11:31.041: W/ViewRootImpl(6633): Cancelling event due to no window focus: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_3, scanCode=4, metaState=0, flags=0x28, repeatCount=0, eventTime=81457114, downTime=81457113, deviceId=36, source=0x101 }
答案 0 :(得分:0)
在keyListener之外创建一个类方法。然后在关键监听器内部,关闭进度对话框。
lectura.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent keyevent) {
pDialog = new ProgressDialog(ValidacionBlueTooth.this);
pDialog.setMessage("Sincronizando...");
pDialog.setCancelable(false);
pDialog.show()
if ((keyevent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
if (pDialog.isShowing()) {
dismissDialog();
}
Intent intent = new Intent(ValidacionBlueTooth.this, Validador.class);
startActivity(intent);
}
}
private void dismissDialog() {
pDialog.dismiss();
}