简体   繁体   English

键盘仅在用户单击EditText时出现

[英]Keyboard only appears when user clicks on EditText

I have the following xml file in my android app . 我的android app有以下xml文件。 When user opens the app, the keyboard becomes active at the same time. 当用户打开应用程序时,键盘将同时处于活动状态。 Even though user has not even started in the EditText . 即使用户甚至还没有启动EditText

How could I control keyboard? 如何控制键盘? I want keyboard to appear when a user tap on the EditText . 我希望当用户点击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: 修改您的axml文件,如下所示:

<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 如此处所述: 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 还要检查此线程以清除对触摸外部的关注: EditText,清除对触摸外部的关注

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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