【发布时间】:2012-11-15 01:04:52
【问题描述】:
我正在使用特定中间件(不重要)在 Android 中开发 TV-Remote Simulator 应用。
在音量按钮(音量+和音量-)的情况下,我要做的是在按下按钮时重复发送“音量调高”信号。
这就是我最后尝试的(其中一个按钮的代码,另一个必须相同,除了名称):
-
声明一个布尔变量
boolean pressedUp = false; -
使用以下 AsyncTask 声明了一个内部类:
class SendVolumeUpTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... arg0) { while(pressedUp) { SendVolumeUpSignal(); } return null; }}
-
为按钮添加监听器:
final Button buttonVolumeUp = (Button) findViewById(R.id.volup); buttonVolumeUp.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if(pressedUp == false){ pressedUp = true; new SendVolumeUpTask().execute(); } case MotionEvent.ACTION_UP: pressedUp = false; } return true; } });
我也尝试过使用Increment-Decrement counter while button is pressed 中的简单线程,但都没有成功。该应用程序可以很好地处理其余按钮(通道等),但完全忽略了音量变化。
【问题讨论】:
标签: android button touch-event ontouchlistener