我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
当前回答
在Manifest文件的下面一行添加您提到的活动
android:windowSoftInputMode="stateAlwaysHidden"
其他回答
真正的问题是你根本不想让它有焦点吗?或者你不希望它显示虚拟键盘,因为它专注于EditText?我并不认为EditText在开始时有什么问题,但当用户没有明确要求关注EditText(并因此打开键盘)时,打开softInput窗口肯定是个问题。
如果是虚拟键盘的问题,请参阅AndroidManifest.xml<activity>元素文档。
android:windowSoftInputMode=“stateHidden”-输入活动时始终隐藏它。
或android:windowSoftInputMode=“stateUnchanged”-不要更改它(例如,如果它还没有显示,则不要显示它,但如果它在输入活动时处于打开状态,则保持它处于打开状态)。
在父布局(例如LinearLayout或ConstraintLayout)中添加标签android:focusableInTouchMode=“true”和android:focusable=“true”将解决该问题。
<!-- 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"/>
我找到的唯一解决方案是:
创建LinearLayout(我不知道其他类型的Layout是否有效)设置属性android:focusable=“true”和android:focuableInTouchMode=“true“
并且EditText在开始活动后无法获得焦点
以下内容将阻止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;
}
});
因为您返回的是false,即不使用事件,所以聚焦行为将照常进行。
<TextView
android:id="@+id/textView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
style="@android:style/Widget.EditText"/>