简体   繁体   中英

Keyboard only appears when user clicks on EditText

I have the following xml file in my android app . When user opens the app, the keyboard becomes active at the same time. Even though user has not even started in the EditText .

How could I control keyboard? I want keyboard to appear when a user tap on the EditText .

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mList"
        android:minWidth="25px"
        android:minHeight="25px">
    <EditText
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search" />
   <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/mListView" />
   </LinearLayout>

Modify your axml file as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mWellList"
    android:minWidth="25px"
    android:minHeight="25px"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true" >

or do it with code:

var root = FindViewById<LinearLayout>(Resource.Id.mWellList);
root.RequestFocus();

as described here: https://forums.xamarin.com/discussion/1856/how-to-disable-auto-focus-on-edit-text

To hide the Keyboard:

Include:

 using Android.Views.InputMethods;  

and then:

InputMethodManager imm = (InputMethodManager) this.GetSystemService(Context.InputMethodService); 
imm.HideSoftInputFromWindow(YourEditTextHere.WindowToken, 0);

Also check this thread for clearing focus on touch outside: EditText, clear focus on touch outside

Add in the manifest file inside the activity tag

android:windowSoftInputMode="stateAlwaysHidden"

like Below

<activity
  android:name=".MainActivity"
  android:label="@string/app_name"
  android:windowSoftInputMode="stateAlwaysHidden" >
  <intent-filter>
     <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM