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

编辑文本列表视图

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

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

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


当前回答

由于我不喜欢用与功能相关的东西来污染XML,所以我创建了这个方法,它“透明地”从第一个可聚焦视图中窃取焦点,然后确保在必要时删除它自己!

public static View preventInitialFocus(final Activity activity)
{
    final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
    final View root = content.getChildAt(0);
    if (root == null) return null;
    final View focusDummy = new View(activity);
    final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View view, boolean b)
        {
            view.setOnFocusChangeListener(null);
            content.removeView(focusDummy);
        }
    };
    focusDummy.setFocusable(true);
    focusDummy.setFocusableInTouchMode(true);
    content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
    if (root instanceof ViewGroup)
    {
        final ViewGroup _root = (ViewGroup)root;
        for (int i = 1, children = _root.getChildCount(); i < children; i++)
        {
            final View child = _root.getChildAt(i);
            if (child.isFocusable() || child.isFocusableInTouchMode())
            {
                child.setOnFocusChangeListener(onFocusChangeListener);
                break;
            }
        }
    }
    else if (root.isFocusable() || root.isFocusableInTouchMode())
        root.setOnFocusChangeListener(onFocusChangeListener);

    return focusDummy;
}

其他回答

您可以将Editext设置为禁用焦点属性,现在这可以通过两种方式应用:

您可以禁用focusable作为常规属性或者您可以禁用FocusableInTouchMode作为触摸模式(触摸屏)中该视图的特定属性

默认情况下,如果Editext位于该活动中视图堆栈的顶部(例如,标头),则focusable属性为true,那么它将在活动启动时可聚焦。

要禁用Focusable,只需将其布尔值设置为false即可。

因此,这将是:

android:focusable="false"

要禁用FocusableInTouchMode,只需将其布尔值设置为false即可。因此,这将是:

android:focusable="false"

您只需找到要应用更改的Textview,然后将相应的代码片段添加到xml文件中的xml规范中。

或者,您可以单击布局编辑器中的Textview,找到显示该Textview所有xml属性的侧边栏,然后简单地向下滚动到声明“Focusable”和“FocusableInTouchMode”的位置,并检查它们是否为true或false。

在onCreate()中禁用它

final KeyListener edtTxtMessageKeyListener = edtTxtMessage.getKeyListener();
edtTxtMessage.setCursorVisible(false);
edtTxtMessage.setKeyListener(null);

最后在EditText的onClick()中启用它

edtTxtMessage.setCursorVisible(true);
edtTxtMessage.setKeyListener(edtTxtMessageKeyListener);

但问题是,我们必须在第一次使用OnScreenKeyboard时单击两下。

@变通办法

InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

也可以在onClick()中尝试:)

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

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

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

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

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

来源