Android中Activity中某个EditText被clearFocus后其他控件(别的EditText)也不要获得焦点

本文介绍如何防止Android中EditText失去焦点后焦点自动跳转到其他可编辑控件的方法。通过设置父级LinearLayout的focusable和focusableInTouchMode属性为true,使焦点停留在父控件上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from:http://www.crifan.com/android_activity_edittext_after_clearfocus_other_control_not_get_focus/

【问题】

当然Activity中,当某个EditText被clearFocus后,结果focus又跑到当前页面中第一个可以获得焦点的控件:别的EditText

此处希望别的没有任何控件去获得焦点。

【折腾过程】

1.搜:

android edittext clearFocus got focus again

参考:

使用TextView/EditText应该注意的地方 – xiaoguozi’s Blog – C++博客

待会可以去试试:

requestFocus

2.搜:

android clearFocus get focus again

参考:

android – How to remove focus without setting focus to another control? – Stack Overflow

其是间接实现:

当取消了一个EditText的focus后,不要让焦点跑到其他控件(尤其是另外的,或者是界面中第一个EditText)

则可以:

对于EditText的parent级别的,一般都是LinearLayout,对他们设置:

?
1
2
android:focusable="true"
android:focusableInTouchMode="true"

这样,当取消了子控件EditText的focus后,focus就自动跑到上一级的view了,即此处的LinearLayout了。

所以去试试:

?
1
2
3
4
5
6
7
8
9
LinearLayout singleTabAllGroup = new LinearLayout(mTabContext);
singleTabAllGroup.setLayoutParams(
   new LayoutParams(
     LayoutParams.MATCH_PARENT,
     LayoutParams.WRAP_CONTENT));
singleTabAllGroup.setOrientation(LinearLayout.VERTICAL);
//singleTabAllGroup.setOrientation(LinearLayout.HORIZONTAL);
singleTabAllGroup.setPadding( 4 , 4 , 4 , 4 );
singleTabAllGroup.setBackgroundColor(getResources().getColor(R.color.TabPageBackgroudColor));

变为:

?
1
2
3
4
5
6
7
8
9
10
11
12
LinearLayout singleTabAllGroup = new LinearLayout(mTabContext);
singleTabAllGroup.setLayoutParams(
   new LayoutParams(
     LayoutParams.MATCH_PARENT,
     LayoutParams.WRAP_CONTENT));
singleTabAllGroup.setOrientation(LinearLayout.VERTICAL);
//singleTabAllGroup.setOrientation(LinearLayout.HORIZONTAL);
singleTabAllGroup.setPadding( 4 , 4 , 4 , 4 );
singleTabAllGroup.setBackgroundColor(getResources().getColor(R.color.TabPageBackgroudColor));
//for got lost focus from child EditText
singleTabAllGroup.setFocusable( true );
singleTabAllGroup.setFocusableInTouchMode( true );

看看效果:

然后基本实现了所需要的效果:

当子的EditText失去焦点后,父级的LinearLayout好像是获得了焦点。

3.不过,对于其他级别的view,也去添加了对应的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:baselineAligned= "false"
     android:orientation= "vertical"
     android:focusable= "true"
     android:focusableInTouchMode= "true" >
 
     ......
 
</LinearLayout>

和:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:paddingLeft= "20sp"
     android:orientation= "horizontal"
     android:focusable= "true"
     android:focusableInTouchMode= "true"
     >
 
......
 
</RelativeLayout>

 

【总结】

想要一个EditText失去焦点后,其他控件(尤其是别的可以编辑的EditText)不要获得焦点,则可以:

对于该EditText的父级控件(一般都是LinearLayout),设置对应的focusable和focusableInTouchMode为true:

xml中写法为:

?
1
2
android:focusable="true"
android:focusableInTouchMode="true"

代码中为:

?
1
edittextView.setFocusableInTouchMode( true );

这样,当子级的EditText失去焦点后,父级的LinearLayout就可以获得焦点

->别的(可编辑的EditText)控件就不会获得焦点了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值