在我的 Android 应用程序中,它会自动聚焦我的布局中的第一个 Button,给它一个橙色的轮廓。如何最好用 XML 设置初始焦点,是否可以什么都不设置?
Button
You could use the requestFocus tag:
requestFocus
<Button ...> <requestFocus /> </Button>
I find it odd though that it auto-focuses one of your buttons, I haven't observed that behavior in any of my views.
Set both :focusable and :focusableInTouchMode to true and call requestFocus. It does the trick.
:focusable
:focusableInTouchMode
@Someone Somewhere I used this to clear focus:
editText.clearFocus();
and it helps
@Someone Somewhere, I tried all of the above to no avail. The fix I found is from http://www.helloandroid.com/tutorials/remove-autofocus-edittext-android . Basically, you need to create an invisible layout just above the problematic Button:
<LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px" > <requestFocus /> </LinearLayout>
Use the code below,
TableRow _tableRow =(TableRow)findViewById(R.id.tableRowMainBody); tableRow.requestFocus();
that should work.
I just add this line of code into onCreate():
onCreate()
this.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Problem solved.
I found this worked best for me.
In AndroidManifest.xml <activity> element add android:windowSoftInputMode="stateHidden"
<activity> element
android:windowSoftInputMode="stateHidden"
This always hides the keyboard when entering the activity.
android:focusedByDefault="true"