我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
当前回答
最简单的方法是在Manifest.xml文件的活动标记中添加android:windowSoftInputMode=“stateAlwaysHidden”
其他回答
在父版面中写入此行。。。
android:focusableInTouchMode="true"
请尝试clearFocus()而不是setSelected(false)。Android中的每个视图都具有可聚焦性和可选择性,我认为您只需要清除焦点。
在父布局(例如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"/>
我用提交按钮清除所有焦点
XML文件:
<LinearLayout
...
android:id="@+id/linear_layout"
android:focusableInTouchMode="true"> // 1. make this focusableInTouchMode...
</LinearLayout>
JAVA文件:
private LinearLayout mLinearLayout; // 2. parent layout element
private Button mButton;
mLinearLayout = findViewById(R.id.linear_layout);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLinearLayout.requestFocus(); // 3. request focus
}
});
我希望这对您有所帮助:)
在Activity的onCreate中,只需在EditText元素上添加useclearFocus()。例如
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
如果您想将焦点转移到另一个元素,请使用requestFocus()。例如
button = (Button) findViewById(R.id.button);
button.requestFocus();