onHoverListener doesn't work in Android

与世无争的帅哥 提交于 2019-11-27 18:16:29

问题


In android document, it mentions supporting the "hover" event since 4.0 (ie. API level 14 and up). But somehow, it doesn't work. Even I tried out the sample code in ApiDemo, which is from Android Sample, it didn't work. My current device is Android 4.0.4. Should I upgrade it to 4.2.2?

Sample code is something as below. Did you have a solution to it? Thanks a lot.

Code:


View container = findViewById(R.id.container);
    container.setOnHoverListener(new View.OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    mMessageTextView.setText(Hover.this.getResources().getString(
                            R.string.hover_message_entered_at,
                            event.getX(), event.getY()));
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    mMessageTextView.setText(Hover.this.getResources().getString(
                            R.string.hover_message_moved_at,
                            event.getX(), event.getY()));
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    mMessageTextView.setText(Hover.this.getResources().getString(
                            R.string.hover_message_exited_at,
                            event.getX(), event.getY()));
                    break;
            }
            return false;
        }
    });


回答1:


Hovering requires support from the hardware. The only thing likely to support it is a stylus. It won't work with just your finger.




回答2:


try using OnFocusChangeListener(). PS worked for me




回答3:


I suggest to Turn on 'Accessibility' and 'Explore by touch' feature in settings. When these features are off, the hover action will be treated as touch actions.



来源:https://stackoverflow.com/questions/17909495/onhoverlistener-doesnt-work-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!