Android:EditText 自动打开键盘,如何停止?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10593195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:16:43  来源:igfitidea点击:

Android: EditText turns on keyboard automatically, how to stop?

androidandroid-edittextxamarin.androidxamarin

提问by Ian Vink

I have a simple EditTextover a ListViewdefined below.

我有一个简单EditTextListView定义如下。

When the app runs, the EditText is selected and the keyboard appears.

当应用程序运行时,EditText 被选中并出现键盘。

I don't want that to happen. I don't want it selected or the keyboard to appear by default.

我不希望这种情况发生。我不希望它被选中或键盘默认出现。

<EditText
    android:id="@+id/search_box"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="type to search titles"
    android:inputType="text"
    android:maxLines="1"
    android:capitalize="none"
    android:linksClickable="false"
    android:autoLink="none"
    android:autoText="true"
    android:singleLine="true" />
<ListView
    android:id="@+id/DetailsListView"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:background="@color/transparent"
    android:cacheColorHint="@color/transparent"
    android:listSelector="@drawable/list_selector"
    android:fastScrollEnabled="true" />

采纳答案by Ian Vink

I found this to be the solution:

我发现这是解决方案:

Window.SetSoftInputMode (SoftInput.StateAlwaysHidden);

Note: This is in C# not Java for Android

注意:这是在 C# 中而不是在 Android 中的 Java

回答by Dahevos

First the tag is monodroid so it's on C# so the accepted answer is correct.

首先标签是 monodroid,所以它在 C# 上,所以接受的答案是正确的。

But is not what I think the author wants... this trick just hide the keyboard but the editText still have the focus.

但不是我认为作者想要的……这个技巧只是隐藏了键盘,但 editText 仍然是焦点。

To do this both in java and c#, you have to put this in your root Layout :

要在 java 和 c# 中做到这一点,你必须把它放在你的根布局中:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

For example :

例如 :

<?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:background="@color/white"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
              >
....
</LinearLayout>

回答by Kevin Parker

Add this in onCreate

在 onCreate 中添加这个

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

回答by Housefly

try this in layout

在布局中试试这个

<EditText
                    android:id="@+id/myid2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/myid1"
                    android:clickable="true"
                    android:cursorVisible="false"
                    android:focusable="false"
                    android:hint="@string/hint"
                    android:singleLine="true"
                    android:textColor="@color/black"
                    android:textSize="15dp" />

and this in your code

这在你的代码中

    myEditText.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP) {
                //do tasks
            }
            return false;
        }
    });

i dont know if it is of any use. i dont even know what it does. but it solved a similar problem for me. sry if i wasted your time

我不知道它是否有任何用处。我什至不知道它是做什么的。但它为我解决了类似的问题。对不起,如果我浪费了你的时间

回答by Alex

i found my solution , you can try this one and it works perfect.

我找到了我的解决方案,你可以试试这个,它完美无缺。

xml (i set editText focusableInTouchMode to false in defualt):

xml(我在默认情况下将editText focusableInTouchMode设置为false):

       <EditText ... android:focusableInTouchMode="false" .../>

and i changed focusableInTouchMode after touching in java :

我在 java 中触摸后更改了 focusableInTouchMode :

        yourEditText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            yourEditText.setFocusableInTouchMode(true);
            return false;
        }
    });

回答by TomDK

android:windowSoftInputMode="stateHidden" did not work for me. Neither did messing around with the elements focus and other techniques (even setting it as enabled=false shows the keyboard).

android:windowSoftInputMode="stateHidden" 对我不起作用。也没有搞乱元素焦点和其他技术(甚至将其设置为 enabled=false 显示键盘)。

My Solution: Create a hideKeyboard Function:

我的解决方案:创建一个 hideKeyboard 函数:

private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

And trigger it whenever the UI that's hosting the textfield becomes visible:

并在托管文本字段的 UI 可见时触发它:

For a Dialog:

对于对话框:

 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            hideKeyboard();
        }

For a widget:

对于小部件:

@Override
public void setVisibility(int visibility) {
    super.setVisibility(visibility);
    hideKeyBoard();
}