失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > android 使布局失去焦点 关于edittext的焦点问题(监听事件/默认没有焦点/获得焦点/

android 使布局失去焦点 关于edittext的焦点问题(监听事件/默认没有焦点/获得焦点/

时间:2023-07-20 07:23:08

相关推荐

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="/apk/res/android"

xmlns:app="/apk/res-auto"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".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); //失去焦点

android 使布局失去焦点 关于edittext的焦点问题(监听事件/默认没有焦点/获得焦点/失去焦点 )...

如果觉得《android 使布局失去焦点 关于edittext的焦点问题(监听事件/默认没有焦点/获得焦点/》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。