android 使布局失去焦点,关于edittext的焦点问题(监听事件/默认没有焦点/获得焦点/失去焦点 )...
1.当edittext的焦点事件改变时,可以通过焦点监听事件来监听焦点改变事件
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
//如果有多个edittext可以用v.getId()来区分
if (hasFocus) {
//获得焦点的操作
} else {
//未获得焦点的操作
}
}
});
2.如何设置edittext默认无焦点,或点击空白处失去焦点
这个问题的解决思路是:在edittext的父布局中加入两行代码夺取焦点:
android:focusable=”true”
android:focusableInTouchMode=”true”
xmlns:android=”http://schemas.android.com/apk/res/android“
xmlns:app=”http://schemas.android.com/apk/res-auto“
xmlns:tools=”http://schemas.android.com/tools“
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”may.com.gittest.MainActivity”
android:focusable=”true”
android:focusableInTouchMode=”true”
>
android:layout_width=”match_parent”
android:id=”@+id/et”
android:layout_height=”wrap_content”/>
edittext获得/失去焦点
mInputView.requestFocus(); //获得焦点
mInputView.setFocusable(false); //失去焦点
还没有评论,来说两句吧...