在EditText上禁用键盘

Fer*_*rox 62 keyboard android android-edittext android-4.0-ice-cream-sandwich

我正在做一个计算器.所以我Buttons用数字和函数做了我自己的.必须计算的表达式是在一个EditText,因为我希望用户也可以在表达式的中间添加数字或函数,所以EditText我有cursor.但我想Keyboard在用户点击时禁用EditText.我发现这个例子可以Android 2.3,但是ICS禁用Keyboard了光标和光标.

public class NoImeEditText extends EditText {

   public NoImeEditText(Context context, AttributeSet attrs) { 
      super(context, attrs);     
   }   

   @Override      
   public boolean onCheckIsTextEditor() {   
       return false;     
   }         
}
Run Code Online (Sandbox Code Playgroud)

然后我NoImeEditText在我的XML文件中使用它

<com.my.package.NoImeEditText
      android:id="@+id/etMy"
 ....  
/>
Run Code Online (Sandbox Code Playgroud)

如何使这个EditText与ICS兼容??? 谢谢.

Ale*_*niy 61

下面的代码是API> = 11和API <11.光标仍然可用.

/**
 * Disable soft keyboard from appearing, use in conjunction with android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
 * @param editText
 */
public static void disableSoftInputFromAppearing(EditText editText) {
    if (Build.VERSION.SDK_INT >= 11) {
        editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
        editText.setTextIsSelectable(true);
    } else {
        editText.setRawInputType(InputType.TYPE_NULL);
        editText.setFocusable(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 使用此禁用方法后如何启用键盘的任何想法? (5认同)

Hip*_*ray 48

是一个可以满足您需求的网站

作为总结,它提供了InputMethodManagerViewAndroid开发者之间的链接.它将引用getWindowToken内部ViewhideSoftInputFromWindow()forInputMethodManager

链接中给出了更好的答案,希望这会有所帮助.

这是一个使用onTouch事件的示例:

editText_input_field.setOnTouchListener(otl);

private OnTouchListener otl = new OnTouchListener() {
  public boolean onTouch (View v, MotionEvent event) {
        return true; // the listener has consumed the event
  }
};
Run Code Online (Sandbox Code Playgroud)

这是同一网站的另一个例子.这声称工作,但似乎是一个坏主意,因为你的EditBox是NULL它将不再是一个编辑器:

MyEditor.setOnTouchListener(new OnTouchListener(){

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    int inType = MyEditor.getInputType(); // backup the input type
    MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
    MyEditor.onTouchEvent(event); // call native handler
    MyEditor.setInputType(inType); // restore input type
    return true; // consume touch even
  }
});
Run Code Online (Sandbox Code Playgroud)

希望这能指出你正确的方向


K_A*_*nas 21

尝试:android:editable="false"android:inputType="none"

  • 这会禁用所有的edittext,所以我无法移动光标=( (5认同)

kue*_*lye 19

您也可以直接在API 21+上使用setShowSoftInputOnFocus(boolean)或通过API 14+上的反射:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    editText.setShowSoftInputOnFocus(false);
} else {
    try {
        final Method method = EditText.class.getMethod(
                "setShowSoftInputOnFocus"
                , new Class[]{boolean.class});
        method.setAccessible(true);
        method.invoke(editText, false);
    } catch (Exception e) {
        // ignore
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个想法很好,但是它不适用于 API 14-15,但适用于 16+。对于 API 15,可以使用名为“setSoftInputShownOnFocus”的方法。 (2认同)

Kan*_*gam 18

editText.setShowSoftInputOnFocus(false);
Run Code Online (Sandbox Code Playgroud)

  • 这个答案比其他答案更适合我的需求,因为我需要我的 EditText 对其他触摸事件(例如“setError”消息)处于活动状态。禁用焦点也会禁用所有触摸事件。 (2认同)

Sur*_*gch 12

禁用键盘(API 11为当前)

这是迄今为止我发现的禁用键盘的最佳答案(我已经看过很多).

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
    editText.setShowSoftInputOnFocus(false);
} else { // API 11-20
    editText.setTextIsSelectable(true);
}
Run Code Online (Sandbox Code Playgroud)

无需使用反射或将其设置InputType为null.

重新启用键盘

以下是根据需要重新启用键盘的方法.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
    editText.setShowSoftInputOnFocus(true);
} else { // API 11-20
    editText.setTextIsSelectable(false);
    editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);
    editText.setClickable(true);
    editText.setLongClickable(true);
    editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
    editText.setText(editText.getText(), TextView.BufferType.SPANNABLE);
}
Run Code Online (Sandbox Code Playgroud)

请参阅此问答,了解为什么需要复杂的pre API 21版本才能撤消setTextIsSelectable(true):

这个答案需要更彻底的测试.

我已经测试了setShowSoftInputOnFocus更高级的API设备,但在下面的@ androiddeveloper评论之后,我发现需要对此进行更全面的测试.

这是一些剪切和粘贴代码,以帮助测试这个答案.如果您可以确认它对API 11到20有效或无效,请发表评论.我没有任何API 11-20设备,我的模拟器出现问题.

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="@android:color/white">

    <EditText
        android:id="@+id/editText"
        android:textColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:text="enable keyboard"
        android:onClick="enableButtonClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:text="disable keyboard"
        android:onClick="disableButtonClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

public class MainActivity extends AppCompatActivity {

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText);
    }

    // when keyboard is hidden it should appear when editText is clicked
    public void enableButtonClick(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
            editText.setShowSoftInputOnFocus(true);
        } else { // API 11-20
            editText.setTextIsSelectable(false);
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            editText.setClickable(true);
            editText.setLongClickable(true);
            editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
            editText.setText(editText.getText(), TextView.BufferType.SPANNABLE);
        }
    }

    // when keyboard is hidden it shouldn't respond when editText is clicked
    public void disableButtonClick(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
            editText.setShowSoftInputOnFocus(false);
        } else { // API 11-20
            editText.setTextIsSelectable(true);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 11

将以下属性添加到布局文件中的Edittext控制器

<Edittext
   android:focusableInTouchMode="true"
   android:cursorVisible="false"
   android:focusable="false"  />
Run Code Online (Sandbox Code Playgroud)

我一直在使用这个解决方案,它对我来说很好.


Jay*_*dri 6

添加到 Alex Kucherenko 解决方案:调用后光标消失的问题setInputType(0)是由于 ICS(和 JB)上的框架错误。

该错误记录在此处:https : //code.google.com/p/android/issues/detail?id=27609

要解决此问题,请setRawInputType(InputType.TYPE_CLASS_TEXT)setInputType通话后立即致电。

要阻止键盘出现,只需覆盖OnTouchListenerEditText 并返回 true(吞下触摸事件):

ed.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return true;
            }
        });
Run Code Online (Sandbox Code Playgroud)

光标出现在 GB 设备上而不是 ICS+ 上的原因让我花了几个小时把头发扯掉,所以我希望这能节省一些人的时间。


Vij*_*jay 6

我发现这个解决方案对我有用.当在正确的位置单击EditText时,它也会放置光标.

EditText editText = (EditText)findViewById(R.id.edit_mine);
// set OnTouchListener to consume the touch event
editText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);   // handle the event first
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  // hide the soft keyboard 
            }                
            return true;
        }
    });
Run Code Online (Sandbox Code Playgroud)


and*_*per 6

在StackOverflow上从多个地方收集解决方案,我认为下一个总结了它:

如果您不需要在活动的任何位置显示键盘,则只需使用下一个用于对话框的标志(从此处获取):

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Run Code Online (Sandbox Code Playgroud)

如果你不想只为特定的EditText,你可以使用它(从这里得到):

public static boolean disableKeyboardForEditText(@NonNull EditText editText) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        editText.setShowSoftInputOnFocus(false);
        return true;
    }
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
        try {
            final Method method = EditText.class.getMethod("setShowSoftInputOnFocus", new Class[]{boolean.class});
            method.setAccessible(true);
            method.invoke(editText, false);
            return true;
        } catch (Exception ignored) {
        }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
        try {
            Method method = TextView.class.getMethod("setSoftInputShownOnFocus", boolean.class);
            method.setAccessible(true);
            method.invoke(editText, false);
            return true;
        } catch (Exception ignored) {
        }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

或者这个(取自这里):

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
           editText.setShowSoftInputOnFocus(false);
       else
           editText.setTextIsSelectable(true); 
Run Code Online (Sandbox Code Playgroud)


Abe*_*efe 5

// only if you completely want to disable keyboard for 
// that particular edit text
your_edit_text = (EditText) findViewById(R.id.editText_1);
your_edit_text.setInputType(InputType.TYPE_NULL);
Run Code Online (Sandbox Code Playgroud)