EditText失去焦点,收起软键盘

目标:点击EditText之外的区域,收起键盘,并且让EditText失去焦点。

贴代码一:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}"
    android:focusable="true"  
    android:focusableInTouchMode="true" >

 <!--   这里有两种办法:
       第一种是RelativeLayout中加入focusable,focusableInTouchMode两种属性
        第二种是 新建一个View如下,可获取焦点
  <View  
    android:layout_width="0dip"  
    android:layout_height="0dip"  
    android:focusable="true"  
    android:focusableInTouchMode="true" /> --> 
    
     <EditText 
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="input something"
         android:id="@+id/editText"
         android:layout_alignParentBottom="true"
        android:focusableInTouchMode="true"/>

</RelativeLayout>

   之前一直不成功的原因在于,我写的xml代码中,EditText是唯一可foucs的View,所以将其clearFocus()之后,焦点仍在EditText中,好像clearFocus()不起作用。 参考博客: http://blog.csdn.net/yzx41099296/article/details/9145647

贴代码二:

public class MainActivity extends Activity {
    private EditText editText;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		editText=(EditText)findViewById(R.id.editText);
		
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if(ev.getAction()==MotionEvent.ACTION_DOWN){
		View v=getCurrentFocus();
		boolean  hideInputResult =isShouldHideInput(v,ev);
		Log.v("hideInputResult","zzz-->>"+hideInputResult);
		if(hideInputResult){
			v.clearFocus();
			InputMethodManager imm = (InputMethodManager) MainActivity.this
					.getSystemService(Activity.INPUT_METHOD_SERVICE);
			if(v != null){
				if(imm.isActive()){
					imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
				}
			}
		}
		}
		return super.dispatchTouchEvent(ev);
	}
	public  boolean isShouldHideInput(View v, MotionEvent event) {
		if (v != null && (v instanceof EditText)) {
			int[] leftTop = { 0, 0 };
			//获取输入框当前的location位置
			v.getLocationInWindow(leftTop);
			int left = leftTop[0];
			int top = leftTop[1];
			int bottom = top + v.getHeight();
			int right = left + v.getWidth();
			//之前一直不成功的原因是,getX获取的是相对父视图的坐标,getRawX获取的才是相对屏幕原点的坐标!!!
			Log.v("leftTop[]","zz--left:"+left+"--top:"+top+"--bottom:"+bottom+"--right:"+right);
			Log.v("event","zz--getX():"+event.getRawX()+"--getY():"+event.getRawY());
			if (event.getRawX() > left && event.getRawX() < right
					&& event.getRawY() > top && event.getRawY() < bottom) {
				// 点击的是输入框区域,保留点击EditText的事件
				return false;
			} else {
				return true;
			}
		}
		return false;
	}
}

贴截图:



  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值