如何在EditText上编辑时禁用键盘弹出?

93

我的应用程序中,所有屏幕的第一个视图都是EditText,因此每次进入屏幕时都会弹出屏幕键盘。如何禁用此弹出并在手动单击EditText时启用它?

    eT = (EditText) findViewById(R.id.searchAutoCompleteTextView_feed);

    eT.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {

            if(hasFocus){
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
            imm.hideSoftInputFromWindow(eT.getWindowToken(), 0);
            }
        }
    });

XML 代码:

<ImageView
    android:id="@+id/feedPageLogo"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:src="@drawable/wic_logo_small" />

<Button
    android:id="@+id/goButton_feed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="@string/go" />

<EditText
    android:id="@+id/searchAutoCompleteTextView_feed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/goButton_feed"
    android:layout_toRightOf="@id/feedPageLogo"
    android:hint="@string/search" />

<TextView
    android:id="@+id/feedLabel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/feedPageLogo"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/feed"
    android:textColor="@color/white" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ButtonsLayout_feed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button
        android:id="@+id/feedButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/feed"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/iWantButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/iwant"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/shareButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/share"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/profileButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/profile"
        android:textColor="@color/black" />
</LinearLayout>

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/feedListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/ButtonsLayout_feed"
    android:layout_below="@id/feedLabel"
    android:textSize="15dp" >
</ListView>

第三个视图(EditText)是当前焦点所在的位置。

29个回答

3

我也遇到了同样的问题,我在XML文件中使用了focusable属性来解决。

<EditText
            android:cursorVisible="false"
            android:id="@+id/edit"
            android:focusable="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

您可能也在寻找安全性方面的解决方案。这也将对此有所帮助。


3

感谢@A.B提供的好方案。

    android:focusableInTouchMode="false"

如果您想禁用EditText中的键盘,只需在EditText标签行中添加android:focusableInTouchMode="false"即可。
适用于Android Studio 3.0.1,最小SDK版本为16,最大SDK版本为26。

1
这是对 A.B 回答的评论吗?还是对原问题的回答?如果这是对 A.B 回答的评论,那么您应该使用 StackOverflow 提供的评论选项,并删除对原问题的回答。 - David Walschots
如果我没有理解答案,对不起。我感谢A.B提供的好答案来解决我的问题。如果我错了,我可以删除那个答案,对不起@DavidWalschots。 - NZXT

2

您需要做的唯一一件事就是在XML中将android:focusableInTouchMode="false"添加到EditText中,就这样。(如果有人仍然需要知道如何以简单的方式完成这项操作)


2

尝试使用以下方法:

EditText yourEditText= (EditText) findViewById(R.id.yourEditText); 
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT); 

结束操作时,您可以使用:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
 imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); 

在您的代码中尝试以下方式:

ed = (EditText)findViewById(R.id.editText1);

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);  

ed.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
        imm.showSoftInput(ed, InputMethodManager.SHOW_IMPLICIT);  
    }
});

它正在以与先前相同的方式工作...在这个EditText视图中,我需要做出任何更改吗??? - Housefly
好的,现在它可以工作了,只需添加一些布局属性... android:clickable="true"
android:cursorVisible="false"
android:focusable="false" 谢谢。
- Housefly
不要做太多事情。将隐藏键盘的代码编写在edittext.setOnFocusChangeListener()方法中并尝试。 - user1213202
如果您能够明确指定何时使用调用hideSoftInput,那么这将是解决方案。在onCreate和onResume之后,键盘会自动弹出。 - ahmet emrah

1
我发现以下模式对于我想要显示一个对话框来获取输入的代码很有效(例如,文本字段中显示的字符串是从对话框中复选框选择的结果,而不是通过键盘输入的文本)。
  1. Disable soft input focus on the edit field. I can't disable for the entire activity, as there are edit fields I do want the keyboard to be used for in the same layout.
  2. Initial clicks in the text field yield a focus change, a repeated click yields a click event. So I override both (here I don't refactor the code out to illustrate both handlers do the same thing):

    tx = (TextView) m_activity.findViewById(R.id.allergymeds);
    if (tx != null) {
        tx.setShowSoftInputOnFocus(false);
        tx.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    MedicationsListDialogFragment mld = new MedicationsListDialogFragment();
                    mld.setPatientId(m_sess.getActivePatientId());
                    mld.show(getFragmentManager(), "Allergy Medications Dialog");
                }
            }
        });
        tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MedicationsListDialogFragment mld = new MedicationsListDialogFragment();
                mld.setPatientId(m_sess.getActivePatientId());
                mld.show(getFragmentManager(), "Allergy Medications Dialog");
            }
        });
    }
    

1
       <TextView android:layout_width="match_parent"
                 android:layout_height="wrap_content"

                 android:focusable="true"
                 android:focusableInTouchMode="true">

                <requestFocus/>
      </TextView>

      <EditText android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

1
如果您正在使用Xamarin,您可以添加这个。
Activity[(WindowSoftInputMode = SoftInput.StateAlwaysHidden)]

之后,您可以在OnCreate()方法中添加这一行。
youredittext.ShowSoftInputOnFocus = false;

如果目标设备不支持上述代码,您可以在EditText点击事件中使用下面的代码。
InputMethodManager Imm = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
Imm.HideSoftInputFromWindow(youredittext.WindowToken, HideSoftInputFlags.None);

1
在您的onCreate()方法中使用以下代码-
    editText = (EditText) findViewById(R.id.editText);
    editText.requestFocus();
    editText.postDelayed(new Runnable() {
        public void run() {
            InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.hideSoftInputFromWindow(
                    editText.getWindowToken(), 0);
        }
    }, 200);

0
在我正在构建的 Android 应用中,我在一个水平排列的 LinearLayout 中有三个 EditText。我必须防止软键盘在片段加载时出现。除了在 LinearLayout 上将 focusable 和 focusableInTouchMode 设置为 true 之外,我还必须将 descendantFocusability 设置为 blocksDescendants。在 onCreate 中,我调用了 LinearLayout 的 requestFocus 方法。这样可以防止在创建片段时弹出键盘。
布局 -
    <LinearLayout
       android:id="@+id/text_selector_container"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:weightSum="3"
       android:orientation="horizontal"
       android:focusable="true"
       android:focusableInTouchMode="true"
       android:descendantFocusability="blocksDescendants"
       android:background="@color/black">

       <!-- EditText widgets -->

    </LinearLayout>

onCreate中 - mTextSelectorContainer.requestFocus();

0

尝试这个答案,

editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);

注意:仅适用于API 11及以上版本


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接