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

编辑文本列表视图

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

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

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


当前回答

当按下按钮时,我使用以下代码阻止EditText窃取焦点。

addButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        View focused = internalWrapper.getFocusedChild();
        focused.setVisibility(GONE);
        v.requestFocus();
        addPanel();
        focused.setVisibility(VISIBLE);
    }
});

Basically, hide the edit text and then show it again. This works for me as the EditText is **not** in view so it doesn't matter whether it is showing.

You could try hiding and showing it in succession to see if that helps it lose focus.

其他回答

我用提交按钮清除所有焦点

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

            }
        });

我希望这对您有所帮助:)

我需要以编程方式明确关注所有领域。我刚刚在主布局定义中添加了以下两条语句。

myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);

就这样。立刻解决了我的问题。谢谢,西尔弗,你给我指明了正确的方向。

这些解决方案都不适合我。我修复自动对焦的方法是:

<activity android:name=".android.InviteFriendsActivity"
 android:windowSoftInputMode="adjustPan">
    <intent-filter >
    </intent-filter>
</activity>

我遇到了这样的问题,这是由于我的选择。第一个状态是焦点,即使我的视图被禁用了,它也采用了焦点状态,因为它是第一个匹配并使用它的状态。您可以像这样将第一个状态设置为禁用:

<selector xmlns:android="http://schemas.android.com/apk/res/android">


<item android:drawable="@drawable/text_field_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/text_field_focused" android:state_focused="true"/>
<item android:drawable="@drawable/text_field_normal"/>

在Manifest.xml文件的活动标记中添加android:windowSoftInputMode=“stateAlwaysHidden”。

来源