将onTouchListener包括在内时,OnclickListener不起作用

时间:2018-06-21 05:30:54

标签: java android

在尝试将onTouchListener和onCLickListener都用于对话框中的“确定”按钮时,我遇到了一个小问题。基本上,当我单击“确定”按钮以关闭对话框时,它会识别onTouch,但不会在要关闭对话框的位置执行onClick,因此对话框保持打开状态。我在实现过程中做错了什么?

    questionButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // custom dialog
            final Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.custom_dialog);

            Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
            // if button is clicked, close the custom dialog

            dialogButton.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    setButtonPress(v, event);
                    return true;

                }
            });

            dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();
        }
    });

public void setButtonPress(View v, MotionEvent event){
            int sdk = android.os.Build.VERSION.SDK_INT;
            Button view = (Button) v;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.love_heart_dark));
                    } else {
                        v.setBackground(getResources().getDrawable(R.drawable.love_heart_dark));
                    }
                    break;

                case MotionEvent.ACTION_UP:
                    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.love_heart));
                    } else {
                        v.setBackground(getResources().getDrawable(R.drawable.love_heart));
                    }
                    break;

                case MotionEvent.ACTION_CANCEL: {
                    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.love_heart));
                    } else {
                        v.setBackground(getResources().getDrawable(R.drawable.love_heart));
                    }
                    break;
                }
            }

2 个答案:

答案 0 :(得分:0)

您不能一次在单个视图上使用两个操作。

尝试一下

Boolean b = false;

questionButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // custom dialog
        final Dialog dialog = new Dialog(MainActivity.this);
        dialog.setContentView(R.layout.custom_dialog);

        Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
        // if button is clicked, close the custom dialog

        dialogButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(b==false){
                setButtonPress(v, event);

                b=true;
                return true;
                }
                else{
                //perform on click here
                }
            }
        });

        dialog.show();
    }
});

答案 1 :(得分:0)

尝试一下

        Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
        // if button is clicked, close the custom dialog

        dialogButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                setButtonPress(v, event);
                return false;

            }
        });

        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();