Android App implements three ways to monitor soft keyboard keys

preface:

On Android phones, we sometimes encounter listening to the phone's soft keyboard keys. For example, after entering the URL in the browser, we can click the "go" button in the lower right corner of the soft keyboard to load the URL page; When clicking the search box, click the search symbol key in the lower right corner to search; Or click "done" in the lower right corner after all data are entered to proceed to the next step immediately.

design sketch:

function 1:

Override the dispatchkeyevent (keyevent event) method of the activity to listen for keyeventkey.keycode_ Enter key (OK key in the lower right corner). When this key is pressed, hide the input method soft keyboard, set the EditText content and load the WebView content.

function 2:

Rewriting the method of dispatchkeyevent (keyevent event) feels a bit like a bull's knife. Because we are likely to carry out other tasks in this method, we can use onkeylistener's method to listen to soft keyboard keys.

function 3:

The third method I think can help programmers more accurately judge the key situation in the lower right corner in order to deal with more complex situations. It can help programmers make more detailed operations according to the current e-mail with the "go", "done" and "search" keys.

Change the style of the OK key in the lower right corner of the soft keyboard:

The keys of the soft keyboard input method are not invariable. For example, the "OK" key in the lower right corner will become a key with a search icon when there is a search box, and will become a "go" key when there is a browser address bar. When writing an app, we may also set the "OK" key of the input method according to different situations, The way to change this is to set the imeoptions property of the EditText control to different values (at this time, the Enter key can display different text and patterns).

Digression:

When I wrote this demo, I found a problem with WebView, that is, directly using webview.load (URL) method will pop up the system browser on the mobile phone to access the URL link, rather than the WebView we set. The solution I found is to use webview.setwebviewclient (...) method to ensure that the URL will be loaded on the WebView of activity.

Demo download address: Android softkeyboard_ jb51.rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>