【问题】
当然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)控件就不会获得焦点了。
转载请注明:在路上 » 【已解决】Android中Activity中某个EditText被clearFocus后其他控件(别的EditText)也不要获得焦点