我有一个 Activity 在安卓,有两个要素:

  1. EditText
  2. ListView

当我的 Activity 开始时, EditText 立即已输入关注(闪烁的光标).我不想有任何控制有输入的重点在启动时。我试过:

EditText.setSelected(false);

没有运气。我如何可以说服 EditText 不要选择本身时的 Activity 开始?

有帮助吗?

解决方案

优秀的答案,从吕克和标记但是一个很好的代码样本的缺失。添加标签 android:focusableInTouchMode="true"<LinearLayout> 如下面的例子将解决问题。

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

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext" 
    android:nextFocusLeft="@id/autotext"/>

其他提示

是的实际问题,你只是不希望它具有的重点?或者你不想让它显示的虚拟键盘的结果作为重点的 EditText?我真的不见的一个问题 EditText 具有注重开始,但它绝对是一个问题有softInput窗口打开的时用户没有明确要求把重点放在 EditText (并开放键盘结果)。

如果这就是问题的虚拟键盘见 AndroidManifest.xml <activity> 元素 文件。

android:windowSoftInputMode="stateHidden" -总是隐藏它的时候进入的活动。

android:windowSoftInputMode="stateUnchanged" -不要改变它的(例如不不 显示 如果它是不是已经显示出,但如果它被打开的时候进入的活动,把它打开)。

一个更简单的解决方案的存在。设置这些属性在你父的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true" >

而现在,活动开始时这个主要布局会重点的默认。

此外,我们可以删除重点从儿童的意见,在运行时(例如,整理后的儿童编辑)通过给予关注的主要布局再次这样的:

findViewById(R.id.mainLayout).requestFocus();

好评纪尧姆Perrot:

android:descendantFocusability="beforeDescendants" 似乎是的 默认(整数值为0)。它只是通过增加 android:focusableInTouchMode="true".

真的,我们可以看到, beforeDescendants 设置作为默认的 ViewGroup.initViewGroup() 方法(安卓2.2.2).但不等于0。 ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;

感谢纪尧姆.

唯一的解决办法,我发现是:

  • 创建一个 LinearLayout (我不知道如果其他类型的布局的工作)
  • 设定的属性 android:focusable="true"android:focusableInTouchMode="true"

EditText 不会的焦点之后开始活动

该问题似乎来自一个酒店,我可以只看到的 XML form 的布局。

一定要删除这条线结束时的宣言》内 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>

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

在布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();
}

尝试 time it() 而不是的 setSelected(false).每一个图在安卓有两focusability和选择功能以,我认为你想要的只是清晰的焦点。

以下将停止edittext从把重点放在创造的时候,但是抓住它,当你接触他们。

<EditText
    android:id="@+id/et_bonus_custom"
    android:focusable="false" />

所以你设置的焦false在xml,但关键是在爪哇,其中添加以下监听器:

etBonus.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.setFocusable(true);
        v.setFocusableInTouchMode(true);
        return false;
    }
});

因为你正在回归假,即不消耗的事件,聚焦行为将会继续喜欢正常的。

我尝试了几个答案单独但重点仍然是在EditText.我只是设法解决它通过使用两个以下的解决方案在一起。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mainLayout"
  android:descendantFocusability="beforeDescendants"
  android:focusableInTouchMode="true" >

(参考从银 https://stackoverflow.com/a/8639921/15695 )

和删除

 <requestFocus />

在EditText

(参考从floydaddict https://stackoverflow.com/a/9681809 )

晚但是最简单的答案,只是添加这在父的布局XML。

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

投票,如果它帮助你!快乐码:)

没有这个方案为我工作。我修复的焦是:

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

简单的解决办法:在 AndroidManifestActivity 标签的使用

android:windowSoftInputMode="stateAlwaysHidden"

你可以设定 "聚焦""聚焦在接触模式" 价值的真正的第一个 TextViewlayout.在这种活动开始时的 TextView 将重点,但是,由于其性质,你将看到什么重点在屏幕上当然会有 没有 键盘显示...

以下为我工作在 Manifest.写的,

<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>

我需要明确的重点,从所有领域的编程方式。我只是添加以下两个发言给我的主要布局的定义。

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

就是这样。固定我的问题。谢谢,银,指着我的方向是正确的。

添加 android:windowSoftInputMode="stateAlwaysHidden" 在"活动"标签的 Manifest.xml 文件。

来源

如果你有另一种看法在您的活动喜欢的一个 ListView, 你也可以做到:

ListView.requestFocus(); 

在你 onResume() 抓住重点从 editText.

我知道这个问题已经回答了但是只是提供一个替代解决方案,为我工作:)

添加下面 onCreate 方法:

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

试试这个之前,你的第一个编辑领域:

<TextView  
        android:id="@+id/dummyfocus" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/foo"
        />

----

findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();

是,我不喜欢污染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;
}

晚了,但也许有帮助的。创建一个虚拟EditText在你的布局,然后打电话 myDummyEditText.requestFocus()onCreate()

<EditText android:id="@+id/dummyEditTextFocus" 
android:layout_width="0px"
android:layout_height="0px" />

这似乎表现为我的期望。没有需要处理结构的变化等。我需要这一活动的冗长的控(说明)。

是啊我做同样的事情-创建一个"哑"线性布局,获取最初的焦点。此外,我设定的"下一步"的重点IDs,使用户可以不着重任何更多的滚动后的一次:

<LinearLayout 'dummy'>
<EditText et>

dummy.setNextFocusDownId(et.getId());

dummy.setNextFocusUpId(et.getId());

et.setNextFocusUpId(et.getId());

很多的工作只是为了摆脱的重点放在查..

感谢

对我来说,什么工作的所有设备上的是这个:

    <!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->

    <View
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true"
    android:focusableInTouchMode="true" />

只要把这个作为一个前景问题的重视,这是它。

写入这条线在你父的布局...

 android:focusableInTouchMode="true"

这是一个完美的和最最简单的解决方案。我总是用我的程序。

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

最简单的事情我所做的是设定专注于另一种看法在onCreate:

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

此停止的软盘来了,有没有闪烁的光标在EditText.

写入这个代码 Manifest 文件在 Activity 在那里你不想开放键盘上。

android:windowSoftInputMode="stateHidden"

清单文件:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.projectt"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="24" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
            android:name=".Splash"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Login"
            **android:windowSoftInputMode="stateHidden"**
            android:label="@string/app_name" >
        </activity>

    </application>

</manifest>
<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 你的活动,只是增加使用 clearFocus() 你EditText元。例如,

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

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

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

你可以实现这种通过创建一个虚拟的 EditText 与布局的宽度和高度设为 0dp, 和请求关注这一观点。添加以下一段代码在xml布局:

<EditText
    android:id="@+id/editText0"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:hint="@string/dummy"
    android:ems="10" 
    >
     <requestFocus />
    </EditText>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top