1. 程式人生 > >android 禁止系統軟鍵盤,攔截鍵盤事件

android 禁止系統軟鍵盤,攔截鍵盤事件

在Edittext中設定
.setInputType(InputType.TYPE_NULL);

public void disableShowInput(){
if (android.os.Build.VERSION.SDK_INT <= 10){
editText.setInputType(InputType.TYPE_NULL);
}else {
Class<EditText> cls = EditText.class;
Method method;
try {
method = cls.getMethod("setShowSoftInputOnFocus"
,boolean.class); method.setAccessible(true); method.invoke(editText,false) }catch (Exception e) {//TODO: handle exception } try { method = cls.getMethod("setSoftInputShownOnFocus",boolean.class); method.setAccessible(true); method.invoke(editText,false); }catch (Exception e) {//TODO: handle exception } } }

攔截鍵盤事件

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK) { //監控/攔截/遮蔽返回鍵
        processExit();
        return true;
    } else if(keyCode == KeyEvent.KEYCODE_MENU) {
        //監控/攔截選單鍵
    } else if(keyCode == KeyEvent.KEYCODE_HOME) {
        //由於Home鍵為系統鍵,此處不能捕獲,需要重寫onAttachedToWindow()
} return super.onKeyDown(keyCode, event); }

參考文章
https://blog.csdn.net/sinat_27672523/article/details/56839837