Explore how to hide the keyboard on the Android interface (and how to return a null value for findViewById), and hide the keyboard on android

Source: Internet
Author: User

Explore how to hide the keyboard on the Android interface (and how to return a null value for findViewById), and hide the keyboard on android

Recently written APP, the teacher said that although my login interface has ScrollView sliding, but the user experience is not very good, because the keyboard will block the input box or login button ( Pad, landscape screen, when only numbers are specified, the down arrow of the built-in keyboard is disabled ).

Although I think ScrollView is enough, you can find the logon button ......

First, after the activity is loaded, the keyboard automatically pops up when EditText is blocked. One sentence is required:

1 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Of course, it must be used after setContentView;

Next, let's give the root layout id and click:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: id = "@ + id/user_info_add" 4 android: layout_width = "match_parent" 5 android: layout_height = "match_parent" 6 android: background = "@ color/white_color" 7 android: clickable = "true" 8 android: orientation = "vertical"> 9 10 <! -- Omitted in the middle --> 11 12 </LinearLayout>

Then add the response code to the corresponding Activity and click the response:

1 ll_login = (LinearLayout) findViewById (R. id. ll_activity_login); 2/* click the blank area to close the soft keyboard */3 ll_login.setOnClickListener (new OnClickListener () {4 @ Override 5 public void onClick (View v) {6 switch (v. getId () {7 case R. id. ll_activity_login: 8 InputMethodManager imm2 = (InputMethodManager) 9 getSystemService (Context. INPUT_METHOD_SERVICE); 10 imm2.hideSoftInputFromWindow (v. getWindowToken (), 0); 11 break; 12} 13 14} 15 });

I thought it was a success, but I found that the keyboard cannot be closed after clicking it!

Check again. The problem lies in ScrollView, which shields such click events. The solution is not none, but the several items found have some impact on the ScrollView and internal component click operations ( ̄ _  ̄ |)

Since the keyboard can be hidden, how can you use it?

Finally, test it. No problem. Everything is normal ~

 

PS: the soft keyboard is not difficult, but it cannot hold any strange ERROR:

① The findViewById statement fails to run. It is not under him, but fails:

Solution: IDE problems (eclipse ...... Ah): Project → Clean;

② FindViewById is running. His next setOnClickListener fails to run:

Solution: Debug it and find that the value of ll_login is null. In the online search, clean is ignored. The position of setContentView is correct, and the View is the current view ......

Someone said they forgot the id defined in xml. I just finished it ...... Wait! I wrote an app for mobile phone flat, which is defined for both horizontal and vertical screens. It seems that I only changed the port and did not change it to the one in land ......

After the change, debug has a value. Everything is normal, crying ...... I got stuck for n hours.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.