问题
In my android app, I have 3 EditTexts in my one activity. I created my own number pad in the activity. Whenever i tap any EditText, soft keyboard comes up. I want to block that permanently for this activity but if user tap an EditText then it should be in focus. Like a cursor blinking. Any idea how can i do that? Thanks in advance.
回答1:
Hiding Keyboard Manually Here
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
YourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
YourEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
});
回答2:
i check my self. its working correct. InputMethodManager imm ;
edittext1 = (EditText) findViewById(R.id.editText1);
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
edittext1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);
}
});
回答3:
In place of setting hideSoftInputFromWindow() to each EditText, it will be good if you set the parameter for the parent layout of the activity. Suppose the parent layout of the activity is a LinearLayout, Then,
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
/* Hide keyboard from this activity permanently */
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(linearLayout.getWindowToken(),0);
Or You can also implement the same thing for xml
<EditText
android:focusable="false"
.../>
This disables the keyboard permanently for that edittext
回答4:
Android studio suggested the following command that keeps the keyboard from showing up. I went ahead and threw it into a function as follows and called it for each EditText object.
private void hideKeyboard(EditText editText){
editText.setShowSoftInputOnFocus(false);
}
来源:https://stackoverflow.com/questions/19046241/is-there-any-way-to-permanently-hide-soft-keyboard-but-keep-enabling-focus-for-a