Search code examples
androidkeyboardandroid-edittextandroid-4.0-ice-cream-sandwich

Disable keyboard on EditText


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

And then I use this NoImeEditText in my XML file

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

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


Solution

  • Here is a website that will give you what you need

    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.

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

    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