Android 如何在 Activity 启动时将焦点设置在 TextView 上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3496720/
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
How to set focus on a TextView when an Activity starts?
提问by user256239
There are an EditText and a TextView in my Activity. I want to set focus on the TextView when the Activity starts. I used the following code:
我的活动中有一个 EditText 和一个 TextView。我想在 Activity 启动时将焦点设置在 TextView 上。我使用了以下代码:
TextView myTextView = (TextView) findViewById(R.id.my_text_vew);
myTextView.setFocusable(true);
myTextView.setOnClickListener(this);
myTextView.requestFocus();
But the code doesn't work. The EditText always receives focus when the activity starts.
但是代码不起作用。EditText 始终在活动开始时获得焦点。
Can anyone help?
任何人都可以帮忙吗?
Thanks.
谢谢。
采纳答案by Romain Guy
This is expected when you start the Activity using the touch screen. When in "touch mode," only a couple of Views (EditText and ListView) can receive the focus. If you start your application using the trackball you will see the focus on the TextView. What you are seeing is the correct and expected result.
当您使用触摸屏启动活动时,这是预期的。当处于“触摸模式”时,只有几个视图(EditText 和 ListView)可以接收焦点。如果您使用轨迹球启动您的应用程序,您将看到 TextView 上的焦点。您所看到的是正确和预期的结果。
回答by LiTTle
I came across with your question now. Maybe after 2 years you have found a solution. But I write it here for the others that will come across with this in the future. The only thing you have to do is to go to the AndroidManifest.xml
, find the Activity
in which you want this behaviour and add this android:windowSoftInputMode="stateHidden"
. So, at the start of your Activity
no keyboard will be shown, until you touch the EditText
object.
我现在遇到了你的问题。也许2年后你找到了解决办法。但我写在这里是为了将来会遇到这个问题的其他人。您唯一要做的就是转到AndroidManifest.xml
,找到Activity
您想要此行为的 并添加此android:windowSoftInputMode="stateHidden"
。因此,在您开始时Activity
不会显示键盘,直到您触摸该EditText
对象。
回答by tomatosoup
If anyone is still wondering, you can use the following in your layout xml to make a textview focusable in touch mode:
如果有人仍然想知道,您可以在布局 xml 中使用以下内容使 textview 在触摸模式下可聚焦:
android:focusableInTouchMode="true"
回答by VIjay J
In order to request focus to TextView you need to set focusable="true"
and focusableInTouchMode="true"
as follows:
为了请求焦点TextView的需要设置focusable="true"
和focusableInTouchMode="true"
如下:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Heading"
android:textSize="20sp"
android:textStyle="bold"
android:focusable="true"
android:focusableInTouchMode="true">
<requestFocus />
</TextView>
回答by App Coder
Use this:
用这个:
EditText mEditText = (EditText) findViewById(R.id.edit_text);
TextView myTextView = (TextView) findViewById(R.id.my_text_vew);
mEditText.setFocusable(false);
myTextView.setFocusable(true);
This takes focus off of the EditText andputs it on the TextView.
这需要重点关的EditText的,并把它放在TextView的。
回答by Joy
Add the TextView Property in layout
在布局中添加 TextView 属性
android:focusable="false"
It's work for me.
这对我有用。
回答by Suhail Parvez
You can set focus to any widget or a TextView from the xml of the activity. Just add requestFocusbefore the TextView end.
您可以将焦点设置到活动的 xml 中的任何小部件或 TextView。只需 在 TextView 结束之前添加requestFocus 即可。
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView2"
android:layout_width="470dp"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_toEndOf="@+id/textView2"
android:ems="10"
android:textColor="#455A64"
android:textColorHint="#455A64"
android:textSize="27sp">
<requestFocus />
</AutoCompleteTextView>