英文:
Show one message when a button is pressed and other one when is released. Android
问题
我只是在尝试编写一个简单的代码,当按钮被按下时显示一条消息,当按钮被释放时显示另一条消息。
我尝试了以下代码,但没有成功:
btnOn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mmHandler.postDelayed(mAction1, 10);
break;
case MotionEvent.ACTION_UP:
mmHandler.removeCallbacksAndMessages(null);
mmHandler.postDelayed(mAction1, 10);
txtdebug.setText("Message 2");
break;
}
return true;
}
Runnable mAction1 = new Runnable() {
@Override
public void run() {
txtdebug.setText("Message 1");
//mHandler.postDelayed(this, 100);
}
};
任何关于为什么 removeCallbacksAndMessages 不起作用的想法。例如,当我释放按钮时,它会显示 `Message 2`,但之后又出现了 `message 1`。它没有保持在那种状态。
英文:
I´m just trying to program a simple code that shows a message when the button is pressed and another one when is released.
I try this but it didn´t work:
btnOn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mmHandler.postDelayed(mAction1, 10);
break;
case MotionEvent.ACTION_UP:
mmHandler.removeCallbacksAndMessages(null);
mmHandler.postDelayed(mAction1, 10);
txtdebug.setText("Message 2");
break;
}
return true;
}
Runnable mAction1 = new Runnable() {
@Override
public void run() {
txtdebug.setText("Message 1");
//mHandler.postDelayed(this, 100);
}
};
Any idea of why the removeCallbacksAndMessages is not working. It displays for instance Message 2
when I released but then it appears again message 1
. It didn´t keep like that.
答案1
得分: 1
我不确定我是否正确理解您,但如果您想在按钮按下时每隔`X`毫秒执行一次函数,然后在用户释放按钮时停止它,您可以这样做(您离这个解决方案非常近,但在`UP`事件中再次运行了Hendler):
public class MainActivity extends AppCompatActivity
{
Button btnOn;
Handler handler;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOn = findViewById(R.id.btnOn);
handler = new Handler();
final Runnable action = new Runnable()
{
@Override
public void run()
{
Log.d("MyTag", "Run action");
handler.postDelayed(this, 100); // 每100毫秒运行一次动作
}
};
btnOn.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d("MyTag", "Down");
handler.postDelayed(action, 10);
return true;
case MotionEvent.ACTION_UP:
Log.d("MyTag", "Up");
handler.removeCallbacksAndMessages(null);
return true;
default:
return false;
}
}
});
}
}
英文:
I don't know If I understand You correctly but if You want to execute function every X
milliseconds when the button is pressed and then when the user release button stop it You can do it like this (You were very close to this solution but You run Hendler again in UP
event):
public class MainActivity extends AppCompatActivity
{
Button btnOn;
Handler handler;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOn = findViewById(R.id.btnOn);
handler = new Handler();
final Runnable action = new Runnable()
{
@Override
public void run()
{
Log.d("MyTag", "Run action");
handler.postDelayed(this, 100); // Run action every 100 ms
}
};
btnOn.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d("MyTag", "Down");
handler.postDelayed(action, 10);
return true;
case MotionEvent.ACTION_UP:
Log.d("MyTag", "Up");
handler.removeCallbacksAndMessages(null);
return true;
default:
return false;
}
}
});
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论