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

编辑文本列表视图

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

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

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


当前回答

只需在EditText的父布局中添加android:focuableInTouchMode=“true”,您就可以摆脱这种尴尬的行为。

其他回答

您可以将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。

从EditText in xml文件中删除<requestFocus/>。

<EditText
       android:id="@+id/emailField"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:inputType="textEmailAddress">
    
       //`<requestFocus />` /* <-- remove this tags */
    </EditText>
<EditText
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:id="@+id/etComments"
    android:hint="Comments.."
    android:textSize="14dp"
    android:focusable="false"
    android:textStyle="italic"/>

在不想打开键盘的“活动”中的Manifest文件中编写此代码。

android:windowSoftInputMode="stateHidden"

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.project"
    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>

在Manifest文件的下面一行添加您提到的活动

android:windowSoftInputMode="stateAlwaysHidden"