在 EditText 上禁用键盘 [英] Disable keyboard on EditText

查看:34
本文介绍了在 EditText 上禁用键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing a calculator. So I made my own Buttons with numbers and functions. The expression that has to be calculated, is in an EditText, because I want users can add numbers or functions also in the middle of the expression, so with the EditText I have the cursor. But I want to disable the Keyboard when users click on the EditText. I found this example that it's ok for Android 2.3, but with ICS disable the Keyboard and also the cursor.

public class NoImeEditText extends EditText {

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

   @Override      
   public boolean onCheckIsTextEditor() {   
       return false;     
   }         
}

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

And then I use this NoImeEditText in my XML file

<com.my.package.NoImeEditText
      android:id="@+id/etMy"
 ....  
/>

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

How I can make compatible this EditText with ICS??? Thanks.

推荐答案

这里 是一个可以为您提供所需内容的网站

Here is a website that will give you what you need

作为总结,它提供了来自 Android 开发者的 InputMethodManagerView 的链接.它将引用 ViewhideSoftInputFromWindow() 内部的 getWindowToken 用于 InputMethodManager

As a summary, it provides links to InputMethodManager and View from Android Developers. It will reference to the getWindowToken inside of View and hideSoftInputFromWindow() for InputMethodManager

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

A better answer is given in the link, hope this helps.

这是一个消费 onTouch 事件的例子:

here is an example to consume the onTouch event:

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
  }
};

这是来自同一网站的另一个示例.这声称有效,但似乎是个坏主意,因为您的 EditBox 为 NULL,它将不再是编辑器:

Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox is NULL it will be no longer an editor:

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
  }
});

希望这为您指明了正确的方向

Hope this points you in the right direction

这篇关于在 EditText 上禁用键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆