如何在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个回答

0
在编程中,当你想要在任何事件上隐藏屏幕键盘时,请尝试下面的代码行。对我来说它起作用了。
context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );

final InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);

if (inputMethodManager.isAcceptingText())       inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);

0

使用此功能启用和禁用EditText....

InputMethodManager imm;

imm = (InputMethodManager)
getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);

if (isETEnable == true) {

    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    ivWalllet.setImageResource(R.drawable.checkbox_yes);
    etWalletAmount.setEnabled(true);
    etWalletAmount.requestFocus();
    isETEnable = false;
    } 
else {

    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);
    ivWalllet.setImageResource(R.drawable.checkbox_unchecked);
    etWalletAmount.setEnabled(false);
    isETEnable = true;
    }

0

针对Xamarin用户:

[Activity(MainLauncher = true, 
        ScreenOrientation = ScreenOrientation.Portrait, 
        WindowSoftInputMode = SoftInput.StateHidden)] //SoftInput.StateHidden - disables keyboard autopop

0

如果任何人仍在寻找最简单的解决方案,请将以下属性设置为您父布局上的true

android:focusableInTouchMode="true"

例子:

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true">

.......
......
</android.support.constraint.ConstraintLayout>

0
private InputMethodManager imm;

...


editText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.onTouchEvent(event);
        hideDefaultKeyboard(v);
        return true;
    }
});

private void hideDefaultKeyboard(View et) {
  getMethodManager().hideSoftInputFromWindow(et.getWindowToken(), 0);
}

private InputMethodManager getMethodManager() {
        if (this.imm == null) {
            this.imm = (InputMethodManager)  getContext().getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
        }
  return this.imm;
}

-1

可以通过以下方式解决此问题, 无需将editText inputType设置为任何值, 只需添加以下行, editText.setTextIsSelectable(true);


嘿,感谢您撰写答案!然而,由于已经有这么多答案了,如果您将其作为评论写在Ranjith Kumar的答案上,可能会更有帮助。如果不使用“inputType”的效果也很有趣。但是,如果我选择其他答案,似乎知道这一点会很有用,因此即使您坚持将其保留为自己的答案,请考虑在那里留下评论。 - Mark

-1

对于您的活动中的任何TextView(或者创建一个假的空TextView,其中android:layout_width="0dp"android:layout_height="0dp"),并为此TextView添加如下内容: android:textIsSelectable="true"


-1

只需从 XML 文件中删除 <request focus/> 标签即可。


2
请您能详细说明并给出一个例子吗?请参见[答案]。 - Bugs

-1

只需要在布局XML中的特定EditText中添加属性android:focusable="false",然后您就可以为EditText编写点击监听器而不会弹出键盘。


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