当按钮被按下时显示一条消息,当按钮被释放时显示另一条消息。Android

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年9月20日 01:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63971364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定