我有一个 Activity
在Android中,有两个元素:
EditText
ListView
当我的 Activity
开始吧 EditText
立即有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我试过了:
EditText.setSelected(false);
没有运气。我怎么能说服 EditText
什么时候不选择自己 Activity
开始?
我有一个 Activity
在Android中,有两个元素:
EditText
ListView
当我的 Activity
开始吧 EditText
立即有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我试过了:
EditText.setSelected(false);
没有运气。我怎么能说服 EditText
什么时候不选择自己 Activity
开始?
Luc和Mark的优秀答案却缺少一个好的代码示例。添加标签 android:focusableInTouchMode="true"
至 <LinearLayout>
像下面的例子将解决问题。
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
实际问题是你根本不希望它有焦点吗?或者你不希望它通过聚焦显示虚拟键盘 EditText
?我真的没有看到问题 EditText
专注于开始,但当用户没有明确要求关注时,打开softInput窗口肯定是个问题。 EditText
(并打开键盘)。
如果是虚拟键盘的问题,请参阅 AndroidManifest.xml
<activity>元素 文档。
android:windowSoftInputMode="stateHidden"
- 进入活动时始终隐藏它。
要么 android:windowSoftInputMode="stateUnchanged"
- 不要改变它(例如不要改变它) 显示 如果它尚未显示,但如果在进入活动时打开,则将其保持打开状态)。
存在更简单的解决方案。在父布局中设置这些属性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
现在,当活动开始时,此主要布局将默认获得焦点。
此外,我们可以通过再次将焦点放在主布局上,在运行时从子视图中删除焦点(例如,在完成子编辑之后),如下所示:
findViewById(R.id.mainLayout).requestFocus();
好评 从 Guillaume Perrot:
android:descendantFocusability="beforeDescendants"
似乎是 默认值(整数值为0)。它只是通过添加工作android:focusableInTouchMode="true"
。
真的,我们可以看到了 beforeDescendants
设置为默认值 ViewGroup.initViewGroup()
方法(Android 2.2.2)。但不等于0。 ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;
感谢纪尧姆。
我发现的唯一解决方案是:
android:focusable="true"
和 android:focusableInTouchMode="true"
而且 EditText
在开始活动后不会得到焦点
问题似乎来自于我只能在中看到的财产 XML form
的布局。
确保在声明的末尾删除此行 EditText
XML标签:
<requestFocus />
这应该是这样的:
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
使用其他海报提供的信息,我使用了以下解决方案:
在布局XML中
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="@+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
在onCreate()
private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//get references to UI components
mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}
最后,在onResume()
@Override
protected void onResume() {
super.onResume();
//do not give the editbox focus automatically when activity starts
mAutoCompleteTextView.clearFocus();
mLinearLayout.requestFocus();
}
以下内容将阻止edittext在创建时获得焦点,但在触摸时抓住它。
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
所以你在xml中将focusable设置为false,但关键是在java中,你添加了以下监听器:
etBonus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
因为您返回假,即不消耗该事件,所以聚焦行为将正常进行。
尝试 clearFocus() 代替 setSelected(false)
。 Android中的每个视图都具有可聚焦性和可选择性,我认为您只想清除焦点。
我曾单独尝试过serval回答,但重点仍然是EditText。我只能通过一起使用上述两个解决方案来解决它。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
(参考Silver https://stackoverflow.com/a/8639921/15695 )
并删除
<requestFocus />
在EditText
(参考floydaddict https://stackoverflow.com/a/9681809 )
这些解决方案都不适合我。我修复自动对焦的方式是:
<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
简单解决方案
在 AndroidManifest
在 Activity
标签使用
android:windowSoftInputMode="stateAlwaysHidden"