在我的Android应用程序中,它会自动将焦点定位在我布局中的第一个
Button
上,并给它一个橙色的轮廓。我如何在XML中设置初始焦点,并且可以将其设置为空吗?您可以使用requestFocus
标签:
<Button ...>
<requestFocus />
</Button>
我觉得奇怪的是它会自动聚焦到你的其中一个按钮,我在我的任何视图中都没有观察到这种行为。
@Someone Somewhere,我尝试了以上所有方法都没有成功。我找到的解决方法是从http://www.helloandroid.com/tutorials/remove-autofocus-edittext-android上得到的。基本上,您需要创建一个不可见的布局,放置在有问题的按钮上方:
<LinearLayout android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px" >
<requestFocus />
</LinearLayout>
将:focusable
和:focusableInTouchMode
都设置为true,并调用requestFocus
。这样可以解决问题。
我发现这对我来说效果最好。
在AndroidManifest.xml的<activity>
元素中添加 android:windowSoftInputMode="stateHidden"
这将始终在进入活动时隐藏键盘。
我刚刚在onCreate()
中添加了这一行代码:
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
问题已解决。
请使用以下代码:
TableRow _tableRow =(TableRow)findViewById(R.id.tableRowMainBody);
tableRow.requestFocus();
我曾使用以下方法来清除焦点:
editText.clearFocus();
它有助于
android:focusedByDefault="true"
focusedByDefault
仅在 API 级别 26 及更高版本中使用(当前最低级别为 21)。 - AlexS
setFocusableInTouchMode(true);
。 - Someone Somewhere