Address: http://blog.csdn.net/subaohao/article/details/9043895
One or more edittexts are placed in the activity. when entering the activity, the first edittext will receive the focus. I hope that all edittexts in the activity will not receive the focus by default, what should we do?
Method:
Add a linearlayout before the first edittext, And the request must obtain the focus; otherwise, the result is invalid. A linearlayout should be added on the Internet, but it is not emphasized to be used.
<Requestfocus/> This sentence does not work.
<LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px"> <requestFocus /> </LinearLayout>
Then, do not use <requestfocus/> for all edittexts.
Some people also say that after initialization
EditText的clearFocus(),即:
EditText textIn = (EditText)findViewById(R.id.TextInput); textIn.clearFocus(); textIn.setSelected(false);
试过,但没有效果。
How does Android disable automatic focus Retrieval for edittext?
In the project, when you enter a page, edittext automatically obtains the focus by default.
So how to cancel this default behavior?
I have been searching for it online for a long time. I have listened to a soft keyboard event and called the clearfouse () method, but I have not tested it! You cannot find the corresponding attribute in XML to disable this default behavior.
Solution: find one in the parent control of edittext and set it
android:focusable="true" android:focusableInTouchMode="true"
In this way, the default edittext behavior is truncated!
<LinearLayout style="@style/FillWrapWidgetStyle" android:orientation="vertical" android:background="@color/black" android:gravity="center_horizontal" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/logo" style="@style/WrapContentWidgetStyle" android:background="@drawable/dream_dictionary_logo" /> <RelativeLayout style="@style/FillWrapWidgetStyle" android:background="@drawable/searchbar_bg" android:gravity="center_vertical" > <EditText android:id="@+id/searchEditText" style="@style/WrapContentWidgetStyle" android:background="@null" android:hint="Search" android:layout_marginLeft="40dp" android:singleLine="true" /> </RelativeLayout> </LinearLayout>
I haven't tried these methods, but I think the essence of these methods should be:
Transfers the focus of edittext to other controls. If edittext is in the first place, a hidden control should be defined before it, so that the focus is placed on the hidden control.
I will test all the methods in the future to verify if my ideas are correct !!
How to Make edittext unable to automatically get the focus ()