点击屏幕其他地方让edittext失去焦点并隐藏输入法的实现方法

让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,则表示输入法打开

 










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值