更新的TextView当音量键为pressed [英] Update TextView when volume up key is pressed

查看:133
本文介绍了更新的TextView当音量键为pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新到Android,请帮我得到这个问题的解决方案。

I am new to android, please help me to get the solution for this question.

我需要当用户点击音量按钮,当每一个用户点击数量必须增加像1,2,3,4,5按钮的时间来更新我的Andr​​oid应用程序的TextView ...
为了这个,我做了如下:

I need to update TextView of my android application when the user clicks volume up button, each time when user clicks the button the count has to increment like 1,2,3,4,5... for this I have done as follows

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

这code是工作,但这个问题是非常慢的TextView更新,如果我preSS按钮,然后继续花费一点时间来更新TextView的。是有什么其他方法来做到这一点?

This code is working but the problem with this is textview updates in very slow,if I press the button continuously then it takes little time to update textview .is there any another method to do this ?

推荐答案

尝试更新的TextView里面runOnUiThread():

Try to updated textview inside runOnUiThread() :

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
   switch (event.getKeyCode()) {
     case KeyEvent.KEYCODE_VOLUME_UP:
          if (event.getAction() == KeyEvent.ACTION_DOWN) {
              runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                   int current =  Integer.parseInt((String)counter_view.getText());
                   counter_view.setText(current+1+"");        
                 }
              });
          }
          return true;
     default:
          return super.dispatchKeyEvent(event);
    }
 }

这篇关于更新的TextView当音量键为pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆