Android - EditText在打开抽屉时获得焦点并且永远不会丢失它

时间:2015-04-08 13:57:07

标签: android android-layout android-edittext focus

我有一个工作DrawerLayout,里面有几个元素。抽屉内的所有元素都位于RelativeLayout内。奇怪的部分是抽屉打开时,EditText自动获得焦点,永不丢失。我尝试将android:clickable="true"属性添加到父元素(RelativeLayout)以欺骗焦点问题,但它没有帮助。是什么原因导致了这种行为?

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The first child in the layout is for the main Activity UI-->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:background="#ffffffff">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listViewChat"
            android:layout_centerHorizontal="true"
            android:choiceMode="multipleChoice" />

    </RelativeLayout>

    <!-- Side navigation drawer UI -->
    <RelativeLayout
        android:id="@+id/drawerContainer"
        android:layout_width="270dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="@drawable/splash_background_blur"
        android:clickable="true">

        <!--this custom view serves as a dimmer for the drawer -->
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#73000000"
            android:id="@+id/view1"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <!--this edit text receives focus automatically on drawer open -->
        <!--and never looses focus as long as the drawer is opened -->
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editTextUserName"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="160dp"
            android:cursorVisible="false"
            android:textColor="#FFF"
            android:textSize="20sp"
            android:background="@null"
            android:maxLength="15"
            android:inputType="textNoSuggestions"/>

        <Button
            android:layout_width="220dp"
            android:layout_height="35dp"
            android:text="@string/buttonSelectPhoto"
            android:id="@+id/buttonTakePhoto"
            android:background="@drawable/splash_button"
            android:textColor="#FFF"
            android:layout_marginTop="200dp"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"/>


        <ListView
             android:id="@+id/navList"
             android:layout_width="match_parent"
             android:layout_height="match_parent"/>

    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

左编辑:只是为了稍微提高一点:我在模拟器上测试应用程序,当我在不触及任何东西的情况下显示抽屉时,editText获得焦点(意味着我向右滑动,抽屉是可见的,我在键盘上键入,文本在editText内看到,而没有实际点击它)

3 个答案:

答案 0 :(得分:0)

使用

Manifest.xml的activity标记中的

android:windowSoftInputMode="stateAlwaysHidden"

答案 1 :(得分:0)

我弄明白我的问题是什么。最后ListView导致了问题。我删除了它,现在抽屉的行为符合预期。

答案 2 :(得分:0)

在编辑文本的父布局中添加以下行,这是您案例中的相对布局

 android:descendantFocusability="afterDescendants"

 android:focusableInTouchMode="true"