How to prevent virtual keyboard displaying when long-press on EditText Widget?
I've got an EditText widget in my app which I have set to read-only, and I want to know when the user long-presses on it. I don't want the virtual keyboard to popup if the user taps on this widget.
To stop the virtual keyboard from appearing, I'm using this:
EditText text = (EditText)findViewById(R.id.editText01); editText.setClickable(false);
This works, but I can't get any long-press messages i开发者_如何学Pythonf I use OnLongClickListener().
Does anyone know how I can prevent the popup keyboard to appear, but still get long-press events?
Thanks.
EDITED: I actually tried the first two answers supplied to this question, and neither worked. What I ended up doing, was for the EditText widget I used the following:
editText.setInputType(InputType.TYPE_NULL);
editText.setCursorVisible(false);
editText.setOnLongClickListener(mOnLongClickListener);
The setInputType() method call turns off clicks to the widget. The setOnLongClickListener() routine will handle all long-press events on the widget, and I return a true from that routine which basically consumes the long-press event. So, I now have an EditText widget that won't have the virtual keyboard popup when pressed, but a long-press on that widget will call my listener method.
button.setOnLongClickListener (View.OnLongClickListener l(){
//override the method
//then do this
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
});
Try using this and options available.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
My problem was that when ever my activity started, kepyboard poped up automatically if there is an EditText. So, I used the above in OnCreate(). Try using different options instead of ".SOFT_INPUT_STATE_ALWAYS_HIDDEN"
精彩评论