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

一时失言乱红尘 2022-10-17 13:58 439阅读 0赞

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); //失去焦点

发表评论

表情:
评论列表 (有 0 条评论,439人围观)

还没有评论,来说两句吧...

相关阅读

    相关 失去焦点修改事件

    一 介绍 失去焦点修改事件(onchange)是当前元素失去焦点并且元素的内容发生改变时触发事件处理程序。该事件一般在下拉文本框中使用。   二 应用 用下拉文本框改变

    相关 listView焦点失去

    /\\ \ item布局里有抢占焦点的ratingbar,在其布局中 \ 设置为android:isIndicator=”true”不可点击即可把焦点给listview