在 Android 应用程序中设置初始焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2743559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set initial focus in an Android application
提问by stealthcopter
In my Android application it automatically focuses the first Button
I have in my layout, giving it an orange outline. How can I set the initial focus preferably in XML, and can this be set to nothing?
在我的 Android 应用程序中,它会自动聚焦Button
我在布局中的第一个,给它一个橙色的轮廓。如何最好在 XML 中设置初始焦点,并且可以将其设置为空?
回答by Matthias
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.
我觉得很奇怪,虽然它自动聚焦你的一个按钮,但我没有在我的任何视图中观察到这种行为。
回答by Vince Lasmarias
@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:
@Someone 在某处,我尝试了以上所有方法都无济于事。我发现的修复来自http://www.helloandroid.com/tutorials/remove-autofocus-edittext-android。基本上,您需要在有问题的 Button 上方创建一个不可见的布局:
<LinearLayout android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px" >
<requestFocus />
</LinearLayout>
回答by Ruivo
Set both :focusable
and :focusableInTouchMode
to true and call requestFocus
. It does the trick.
将:focusable
和都设置:focusableInTouchMode
为 true 并调用requestFocus
. 它可以解决问题。
回答by James
I found this worked best for me.
我发现这对我最有效。
In AndroidManifest.xml <activity> element
add android:windowSoftInputMode="stateHidden"
在 AndroidManifest.xml<activity> element
添加android:windowSoftInputMode="stateHidden"
This always hides the keyboard when entering the activity.
这在进入活动时总是隐藏键盘。
回答by Sam Tang
I just add this line of code into onCreate()
:
我只是将这行代码添加到onCreate()
:
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Problem solved.
问题解决了。
回答by Sam Tang
Use the code below,
使用下面的代码,
TableRow _tableRow =(TableRow)findViewById(R.id.tableRowMainBody);
tableRow.requestFocus();
that should work.
那应该工作。
回答by Ruobin Wang
@Someone Somewhere I used this to clear focus:
@Someone 在某处我用它来清除焦点:
editText.clearFocus();
and it helps
它有帮助
回答by TiyebM
android:focusedByDefault="true"