18

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

当应用程序运行时,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" />
4

6 回答 6

29

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

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

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

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>
于 2013-04-03T08:00:22.500 回答
13

我发现这是解决方案:

Window.SetSoftInputMode (SoftInput.StateAlwaysHidden);

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

于 2012-05-15T19:22:56.580 回答
8

Add this in onCreate

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
于 2014-03-07T16:51:30.213 回答
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;
        }
    });

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

于 2012-05-15T03:24:58.790 回答
1

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

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

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

and i changed focusableInTouchMode after touching in java :

        yourEditText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            yourEditText.setFocusableInTouchMode(true);
            return false;
        }
    });
于 2019-01-13T07:15:02.740 回答
0

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).

My Solution: Create a hideKeyboard Function:

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:

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();
}
于 2019-02-08T04:58:52.813 回答