Problem description:
Android set-top box application:
The main interface of the application (MainActivity) has only two controls, a videoview and a button.
The video frame has no focus and the keys have focus.
When you first enter the application, the focus is normal and focuses on the button; but when you click the button and jump to the next activity and return, the focus is focused on the videoview, and the code is forced to set the focus:
();
Ineffective. The phenomenon is that when the video is loading, the focus is at the button, but when the video is loaded and started playing, the entire activity loses focus. After a few seconds, the focus runs onto the videoview.
Cause analysis:
I checked the source code of VideoView and found that the reason was in the initialization:
private void initVideoView() { mVideoWidth = 0; mVideoHeight = 0; getHolder().addCallback(mSHCallback); getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); setFocusable(true);//The problem is here setFocusableInTouchMode(true); requestFocus(); mPendingSubtitleTracks = new Vector<Pair<InputStream, MediaFormat>>(); mCurrentState = STATE_IDLE; mTargetState = STATE_IDLE; }
I suddenly realized this, which explains why android:focusable="false" is set in xml and videoview can still get focus, and also explains why buttons get focus and are preempted by videoview when the video is loaded.
Solution:
It is easy to solve the problem after understanding the problem, because it is just to solve the focus problem, so there is no need to customize the videoview, just rewrite the onResume() method:
@Override protected void onResume() { (); (false); }
Summarize:
Be sure to read more source code!
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.