Close/hide the Android Soft Keyboard and Show custom keyboard

放肆的年华 提交于 2019-12-24 03:16:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!