让EditText所在的layout或者其他layout获得焦点。给layout注册OnTouchListener监听器。
1、先执行下面这两个方法:
.setFocusable(true);
.setFocusableInTouchMode(true);
2、再执行.requestFocus() 获取焦点。
说明:仅仅使用 .requestFocus() 无法获取焦点,焦点依然在EditTtext上。
3、隐藏输入法。(set_search_friend为EditTtext。)
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
// imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
imm.hideSoftInputFromWindow(set_search_friend.getWindowToken(), 0);
//rwb:取消EditText焦点,并且隐藏输入法。
final View ll_search_friend = curView.findViewById(R.id.ll_search_friend);
ll_search_friend.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
ll_search_friend.setFocusable(true);
ll_search_friend.setFocusableInTouchMode(true);
ll_search_friend.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
// imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
imm.hideSoftInputFromWindow(set_search_friend.getWindowToken(), 0);
return false;
}
});
PullToRefreshListView直接调用listView.setOnTouchListener无效的,必须listView.getRefreshableView().setOnTouchListener这样调用。
//rwb:取消EditText焦点,并且隐藏输入法。
listView.getRefreshableView().setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
logger.d("SearchFriendFragment#listView.onTouch");
ll_search_friend.setFocusable(true);
ll_search_friend.setFocusableInTouchMode(true);
ll_search_friend.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
// imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
imm.hideSoftInputFromWindow(set_search_friend.getWindowToken(), 0);
return false;
}
});
===================================================
【关闭输入法的参考方法】
直接关闭输入法:
1
2
3
4
5
6
7
8
|
private
void closeInputMethod() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); boolean
isOpen = imm.isActive(); if
(isOpen) { // imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//没有显示则显示 imm.hideSoftInputFromWindow(mobile_topup_num.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1 、方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput( 0 , InputMethodManager.HIDE_NOT_ALWAYS);
2 、方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示) InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
imm.hideSoftInputFromWindow(view.getWindowToken(),
0 ); //强制隐藏键盘
3 、调用隐藏系统默认的输入法 ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity. this .getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是当前的Activity)
4 、获取输入法打开的状态 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean
isOpen=imm.isActive(); //isOpen若返回true,则表示输入法打开 |