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); (這個方法可以實現輸入法在窗口上切換顯示,
如果輸入法在窗口上已經顯示,則隱藏,如果隱藏,則顯示輸入法到窗口上)
((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,則表示輸入法打開