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

编辑文本列表视图

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

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

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


当前回答

问题似乎来自我只能在布局的XML形式中看到的属性。

确保在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>

其他回答

<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"/>

如果要在活动开始时隐藏键盘。那就提一下

android:windowSoftInputMode="stateHidden"

到清单文件中的该活动。问题得到解决。

干杯

您可以在布局的第一个TextView中将“focusable”和“focustable in touch mode”设置为true。这样,当活动开始时,TextView将被聚焦,但由于其性质,您将不会在屏幕上看到任何聚焦,当然,也不会显示键盘。。。

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

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

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

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

if (current != null) 
    current.clearFocus();