1、EditText有焦点(focusable为true)阻止输入法弹出
Java代码 收藏代码
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)时阻止输入法弹出
Java代码 收藏代码
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
3、显示输入法
Java代码 收藏代码
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(m_receiverView(接受软键盘输入的视图(View)),InputMethodManager.SHOW_FORCED(提供当前操作的标记,SHOW_FORCED表示强制显示));
4、隐藏输入法
Java代码 收藏代码
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是当前的Activity)
5、获取输入法状态
Java代码 收藏代码
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
Java代码 收藏代码
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)时阻止输入法弹出
Java代码 收藏代码
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
3、显示输入法
Java代码 收藏代码
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(m_receiverView(接受软键盘输入的视图(View)),InputMethodManager.SHOW_FORCED(提供当前操作的标记,SHOW_FORCED表示强制显示));
4、隐藏输入法
Java代码 收藏代码
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是当前的Activity)
5、获取输入法状态
Java代码 收藏代码
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
isOpen若返回true,则表示输入法打开
只需让EditText设置以下的OnFocusChangeListener就可以了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
private
OnFocusChangeListener mOnFocusChangeListener =
new
OnFocusChangeListener() {
@Override
public
void
onFocusChange(View v,
boolean
hasFocus)
{
EditText textView = (EditText)v;
String hint;
if
(hasFocus) {
hint = textView.getHint().toString();
textView.setTag(hint);
textView.setHint(
""
);
}
else
{
hint = textView.getTag().toString();
textView.setHint(hint);
}
}
};
|
一进入一个页面, EditText默认就会自动获取焦点。解决之道:找一个EditText的父级控件,设置成
1
2
|
android:focusable=
"true"
android:focusableInTouchMode=
"true"
|