问题
I have make my own keyboard, in my activity there is an option (English-Hindi, Hindi-English) when user choose English-Hindi the default keyboard will be open and hide my custom keyboard or when user select Hindi-English option the default keyboard will be hide and custom keyboard will be opened up
below is my source code,
private EditText mEt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mEt = (EditText) findViewById(R.id.searchText);
enableHindiKeyboard();
hideDefaultKeyboard();
}
For hiding default keyboard
private void hideDefaultKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEt.getWindowToken(), 0);
}
For enabling Hindi Keyboard
public void enableHindiKeyboard() {
try {
for (int i = 0; i < mB.length; i++)
mB[i].setOnClickListener(this);
mBSpace.setOnClickListener(this);
mBdone.setOnClickListener(this);
mBack.setOnClickListener(this);
mBChange.setOnClickListener(this);
mNum.setOnClickListener(this);
mEt.setOnTouchListener(MainActivity.this);
mEt.setOnFocusChangeListener(MainActivity.this);
mEt.setOnClickListener(MainActivity.this);
} catch (Exception e) {
Log.w(getClass().getName(), e.toString());
}
}
in this problem when i start my activity at that time i want Hindi Keyboard should be display and Default keyboard should be invisible but the problem is that
hindi keyboard will not be shown at startup and Default keyboard will popup
回答1:
// try this
public void showSoftKeyboard() {
try {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void hideSoftKeyboard() {
try {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
回答2:
Use this to hide the keyboard imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
and to show the keyboard call it again. Call first the hideDefaultKeyboard();
before enableHindiKeyboard();
Hope it helps.
回答3:
try this
In your AndroidManifest.xml:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
回答4:
try
{
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
catch (Exception e)
{
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/19312003/close-hide-the-android-soft-keyboard-and-show-custom-keyboard