我在Android中有一个活动,包含两个元素:

编辑文本列表视图

当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:

EditText.setSelected(false);
EditText.setFocusable(false);

没有运气。如何说服EditText在“活动”启动时不选择自己?


当前回答

在Activity的onCreate中,只需在EditText元素上添加useclearFocus()。例如

edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();

如果您想将焦点转移到另一个元素,请使用requestFocus()。例如

button = (Button) findViewById(R.id.button);
button.requestFocus();

其他回答

在Activity的onCreate中,只需在EditText元素上添加useclearFocus()。例如

edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();

如果您想将焦点转移到另一个元素,请使用requestFocus()。例如

button = (Button) findViewById(R.id.button);
button.requestFocus();

请尝试clearFocus()而不是setSelected(false)。Android中的每个视图都具有可聚焦性和可选择性,我认为您只需要清除焦点。

隐藏键盘的最简单方法是使用setSoftInputMode

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

或者可以使用InputMethodManager并像这样隐藏键盘。

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

在Manifest文件的下面一行添加您提到的活动

android:windowSoftInputMode="stateAlwaysHidden"

做这个,完成你的工作!

android:focusable="true"
android:focusableInTouchMode="true"