简体   繁体   中英

Show one message when a button is pressed and other one when is released. Android

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM