android EditText和輸入法相關知識總結

Android中EditText使用方法詳解


1、EditText有焦點(focusable爲true)阻止輸入法彈出  

editText=(EditText)findViewById(R.id.txtBody);

        editText.setOnTouchListener(new OnTouchListener() {             

            public boolean onTouch(View v, MotionEvent event) {  

                editText.setInputType(InputType.TYPE_NULL); // 關閉軟鍵盤      

                return false;

            }

        });  

2、當EidtText無焦點(focusable=false)時阻止輸入法彈出 

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

1.調用顯示系統默認的輸入法 

方法一、 

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(m_receiverView(接受軟鍵盤輸入的視圖(View)),InputMethodManager.SHOW_FORCED(
提供當前操作的標記,SHOW_FORCED表示強制顯示));

方法二、

InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); (這個方法可以實現輸入法在窗口上切換顯示,
如果輸入法在窗口上已經顯示,則隱藏,如果隱藏,則顯示輸入法到窗口上)


2.調用隱藏系統默認的輸入法 

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  
(WidgetSearchActivity是當前的Activity)
3.獲取輸入法打開的狀態 

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
boolean isOpen=imm.isActive();  
isOpen若返回true,則表示輸入法打開





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章