我的代码中有这个,而.setPressed不起作用:

Button btndesligado = (Button) findViewById(R.id.button12);
 btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!

    @Override
    public void onClick(View v) {

        btndesligado.setPressed(true);
        som = false;
        vibrar = false;

    }
 });


怎么了?它不会给我任何错误,当我打开它并单击它时它不会起作用。我按一次后,应该按一下。

最佳答案

Android会在您的setPressed之前和之后更改onClickEvent

因此,请更改此代码

btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!

    @Override
    public void onClick(View v) {

        btndesligado.setPressed(true);
        som = false;
        vibrar = false;

    }
 });


对此

 btndesligado.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            // show interest in events resulting from ACTION_DOWN
            if (event.getAction() == MotionEvent.ACTION_DOWN) return true;

            // don't handle event unless its ACTION_UP so "doSomething()" only runs once.
            if (event.getAction() != MotionEvent.ACTION_UP) return false;


            btndesligado.setPressed(true);
            som = false;
            vibrar = false;
            return true;
       }
    });

关于java - setPressed不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38170056/

10-09 05:02