【发布时间】:2014-11-20 12:20:59
【问题描述】:
我是android新手,请帮我解决这个问题。
当用户单击音量增大按钮时,我需要更新我的 android 应用程序的 TextView,每次用户单击该按钮时,计数必须像 1、2、3、4、5... 为此我做了如下操作
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
int current = Integer.parseInt((String)counter_view.getText());
counter_view.setText(current+1+"");
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
此代码正在运行,但问题是 textview 更新速度非常慢,如果我连续按下按钮,则更新 textview 需要很少的时间。有没有其他方法可以做到这一点?
【问题讨论】:
-
嘿...@programr....你注意到了吗,case KeyEvent.KEYCODE_VOLUME_UP: 当你点击音量增大时会被触发..那么你怎么能检查它里面的 if() ?
-
你试过
action == KeyEvent.ACTION_UP吗? -
各位,我对按键事件没有任何问题。仅关于更新 TextView
-
@BadAttemtsFirst 我已经引用了此链接中的代码stackoverflow.com/questions/2874743/…
-
您可以使用
runOnUiThread或AsyncTask直接在UI 线程中更新组件。
标签: android android-asynctask textview