Nice programing

Android 버튼에 setOnTouchListener가 호출되었지만 performClick을 재정의하지 않습니다.

nicepro 2020. 10. 12. 08:10
반응형

Android 버튼에 setOnTouchListener가 호출되었지만 performClick을 재정의하지 않습니다.


onTouchListner()버튼 에 추가하려고 하면

버튼에 setOnTouchListener가 호출되었지만 performClick을 재정의하지 않습니다.

경고. 누구든지 그것을 고치는 방법을 알고 있습니까?

1

btnleftclick.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }
});

오류:

사용자 정의보기가 setOnTouchListener를 호출했지만 performClick을 재정의하지 않습니다. onTouchEvent를 재정의하거나 OnTouchListener를 사용하는보기가 performClick도 구현하지 않고 클릭이 감지 될 때 호출하는 경우보기가 접근성 작업을 제대로 처리하지 못할 수 있습니다. 클릭 동작이 발생해야 할 때 일부 접근성 서비스가 performClick을 호출하므로 클릭 동작을 처리하는 로직은 View # performClick에 배치하는 것이 이상적입니다.


이 경고는 Android가 앱을 사용하고있을 수있는 시각 장애인 또는 시각 장애인에 대해 생각하도록 상기 시키려고하기 때문에 표시됩니다. 그게 어떤 것인지에 대한 간략한 개요 를 보려면 이 비디오시청하는 것이 좋습니다 .

표준 UI보기 (예 Button: TextView, 등)는 모두 시각 장애인에게 접근성 서비스를 통해 적절한 피드백을 제공하도록 설정되어 있습니다. 터치 이벤트를 직접 처리하려고하면 해당 피드백 제공을 잊어 버릴 위험이 있습니다. 이것이 경고의 목적입니다.

옵션 1 : 사용자 지정보기 만들기

터치 이벤트 처리는 일반적으로 사용자 정의보기에서 수행됩니다. 이 옵션을 너무 빨리 닫지 마십시오. 그렇게 어렵지 않습니다. 다음은 TextView터치 이벤트를 처리하기 위해 재정 의 된의 전체 예입니다 .

public class CustomTextView extends AppCompatTextView {

    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                return true;

            case MotionEvent.ACTION_UP:
                performClick();
                return true;
        }
        return false;
    }

    // Because we call this from onTouchEvent, this code will be executed for both
    // normal touch events and for when the system calls this using Accessibility
    @Override
    public boolean performClick() {
        super.performClick();
        doSomething();
        return true;
    }

    private void doSomething() {
        Toast.makeText(getContext(), "did something", Toast.LENGTH_SHORT).show();
    }
}

Then you would just use it like this:

<com.example.myapp.CustomTextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:text="Click me to do something"/>

See my other answer for more details about making a custom view.

Option 2: Silencing the warning

Other times it might be better to just silence the warning. For example, I'm not sure what it is you want to do with a Button that you need touch events for. If you were to make a custom button and called performClick() in onTouchEvent like I did above for the custom TextView, then it would get called twice every time because Button already calls performClick().

Here are a couple reasons you might want to just silence the warning:

  • The work you are performing with your touch event is only visual. It doesn't affect the actual working of your app.
  • You are cold-hearted and don't care about making the world a better place for blind people.
  • 위의 옵션 1에서 제공 한 코드를 복사하여 붙여 넣기에는 너무 게으르다.

경고를 표시하지 않으려면 메소드 시작 부분에 다음 행을 추가하십시오.

@SuppressLint("ClickableViewAccessibility")

예를 들면 :

@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button myButton = findViewById(R.id.my_button);
    myButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return false;
        }
    });
}

해결책:

  1. Button 또는 사용중인 뷰를 확장하는 클래스를 만들고 performClick ()을 재정의합니다.

    class TouchableButton extends Button {
    
        @Override
        public boolean performClick() {
            // do what you want
            return true;
        }
    }
    
  2. 이제이 TouchableButton을 xml 및 / 또는 코드에서 사용하면 경고가 사라집니다!


추가해 보셨나요?

view.performClick()

또는 억제 주석 추가 :

@SuppressLint("ClickableViewAccessibility")

?

참고 URL : https://stackoverflow.com/questions/47107105/android-button-has-setontouchlistener-called-on-it-but-does-not-override-perform

반응형