1. 程式人生 > >關於輸入框與軟鍵盤的那些事

關於輸入框與軟鍵盤的那些事

事情要從第一臺裝置說起,這臺裝置有物理鍵盤,需求就要求無論我怎麼點輸入框都不允許出現軟鍵盤。
於是就在網上各種篩選,最終找到合理的解決方案


public void SetNoInput(Activity activity, EditText et_input)
{
    if (android.os.Build.VERSION.SDK_INT <= 10)
    {
        et_input.setInputType(InputType.TYPE_NULL);
    } else
    {
          activity.getWindow().setSoftInputMode(WindowManager
.LayoutParams. SOFT_INPUT_STATE_ALWAYS_HIDDEN); try { Class<EditText> cls = EditText.class; Method setSoftInputShownOnFocus; setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus",boolean.class); setSoftInputShownOnFocus.setAccessible(true
); setSoftInputShownOnFocus.invoke(et_input, false); } catch (Exception e) { e.printStackTrace(); } } }

然後又有了第二臺裝置,這個裝置又需要彈出軟鍵盤了
1.要求進來後先不彈,點選以後再彈

隱藏軟鍵盤

InputMethodManager imm = (InputMethodManager) activity.
                                       getSystemService(Context
.INPUT_METHOD_SERVICE); boolean isOpen = imm.isActive(); if (isOpen) { // imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);//沒有顯示則顯示 imm.hideSoftInputFromWindow(et_input.getWindowToken(), 0);//隱藏軟鍵盤 }

顯示軟鍵盤

InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);  
imm.showSoftInput(et_input, 0);  

隱藏軟鍵盤

WindowManager.LayoutParams params = activity.getWindow().getAttributes();
activity.getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN;

顯示軟鍵盤

WindowManager.LayoutParams params = activity.getWindow().getAttributes();
activity.getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
params.softInputMode =  WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;