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

时间:2012-05-15 01:45:20

标签: android android-edittext xamarin.android xamarin

我在下面定义的EditText上有一个简单的ListView

当应用运行时,会选择EditText并显示键盘。

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

<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" />

6 个答案:

答案 0 :(得分:29)

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

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

要在java和c#中执行此操作,必须将其放在根布局中:

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

例如:

<?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>

答案 1 :(得分:12)

我发现这是解决方案:

Window.SetSoftInputMode (SoftInput.StateAlwaysHidden);

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

答案 2 :(得分:8)

在onCreate中添加

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

答案 3 :(得分:4)

在布局中尝试这个

<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" />

并在您的代码中

    myEditText.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

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

我不知道它是否有用。我甚至不知道它做了什么。但它为我解决了类似的问题。如果我浪费你的时间sry

答案 4 :(得分:1)

我找到了我的解决方案,您可以尝试一下,效果很好。

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

np.random.choice

在触摸Java之后,我更改了focusableInTouchMode:

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

答案 5 :(得分:0)

android:windowSoftInputMode =“ stateHidden”对我不起作用。 也没有弄乱元素焦点和其他技术(即使将其设置为enabled = false也会显示键盘)。

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

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

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

对于对话框:

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

对于小部件:

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