我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
当前回答
<EditText
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/etComments"
android:hint="Comments.."
android:textSize="14dp"
android:focusable="false"
android:textStyle="italic"/>
其他回答
我需要以编程方式明确关注所有领域。我刚刚在主布局定义中添加了以下两条语句。
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
就这样。立刻解决了我的问题。谢谢,西尔弗,你给我指明了正确的方向。
在父布局(例如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"/>
在父版面中写入此行。。。
android:focusableInTouchMode="true"
View current = getCurrentFocus();
if (current != null)
current.clearFocus();
最迟但最简单的答案,只需将其添加到XML的父布局中即可。
android:focusable="true"
android:focusableInTouchMode="true"
如果对你有帮助,请投票!快乐编码:)