Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

android - How do I prevent the software keyboard from popping up?

I have my own keypad in my application so I want to hide the software keyboard all the time(in specific activities & dialogs). I experimented with two options:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

This code prevent the keyboard from popping up at the beginning, but when I click on the textbox the keyboard still pops.

InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

This code hide the keyboard, but it doesn't prevent the keyboard from popping up.

PLEASE HELP!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally figured out!

I used

public void supressKeyboard() {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
}

for the activities where I want to suppress the keyboard(you can put it in a general activity where all other activities inherit from)

But this won't prevent the keyboard from popping up when you click on the EditText textbox. What I did is I consumed the onTouch event for the text box:

@Override
public boolean onTouchEvent(MotionEvent event) {
    return true;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...