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

编辑文本列表视图

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

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

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


当前回答

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

android:windowSoftInputMode="stateHidden"

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

干杯

其他回答

我找到的唯一解决方案是:

创建LinearLayout(我不知道其他类型的Layout是否有效)设置属性android:focusable=“true”和android:focuableInTouchMode=“true“

并且EditText在开始活动后无法获得焦点

利用其他海报提供的信息,我使用了以下解决方案:

在布局XML中

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:id="@+id/linearLayout_focus"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="0px"
    android:layout_height="0px"/>

<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
    android:id="@+id/autocomplete"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:inputType="textNoSuggestions|textVisiblePassword"/>

在onCreate()中

private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    //get references to UI components
    mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}

最后,在onResume()中

@Override
protected void onResume() {
    super.onResume();

    //do not give the editbox focus automatically when activity starts
    mAutoCompleteTextView.clearFocus();
    mLinearLayout.requestFocus();
}

您有编辑文本和列表。在OnStart/On Create中,应该将焦点设置在listview:listview.requestfocus()上

以下内容将阻止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,即不使用事件,所以聚焦行为将照常进行。

我做的最简单的事情是在onCreate中设置另一个视图的焦点:

myView.setFocusableInTouchMode(true);
myView.requestFocus();

这阻止了软键盘的出现,并且EditText中没有光标闪烁。