如果我按下录制按钮,EditText将失去焦点

时间:2017-03-29 10:58:19

标签: android android-edittext android-recyclerview chat

我正在尝试构建聊天界面。我已经编写了用于录制动画的代码用于fab.my问题是当我按下按钮并且我的edittext聚焦时,edittext无缘无故地失去了焦点,这隐藏了键盘中的自动检测字条,而Fab在上面键盘1厘米。我知道这个问题是由循环视图引起的,因为当我将回收器视图的可见性设置为Gone时,编辑文本在我尝试记录时不会失去焦点。我也尝试过更改windowSoftInputMode。谢谢您的帮助。

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:id="@+id/relativeLayout_chat_background"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"

        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"

        >



    <com.example.fcb.insurance.customViews.holdingbutton.HoldingButtonLayout
        android:id="@+id/input_holder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        app:hbl_cancel_icon="@drawable/ic_clear_white_24dp"
        app:hbl_color="@color/colorAccent"
        app:hbl_holding_view="@+id/start_record"
        app:hbl_icon="@drawable/ic_mic_white_24dp"
        app:hbl_radius="@dimen/activity_main_holding_button_radius"
        app:hbl_second_radius="@dimen/activity_main_holding_button_second_radius"
        android:layout_toLeftOf="@+id/sendMessage"
       android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:clipChildren="false"
        android:clipToPadding="false"        >

        <LinearLayout
            android:id="@+id/slide_to_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|end"
            android:layout_marginEnd="@dimen/activity_main_slide_to_cancel_margin_right"
            android:layout_marginRight="@dimen/activity_main_slide_to_cancel_margin_right"
            android:gravity="center_vertical"
            android:visibility="invisible">

            <ImageView
                android:layout_width="@dimen/activity_main_slide_to_cancel_icon_size"
                android:layout_height="@dimen/activity_main_slide_to_cancel_icon_size"
                android:layout_marginEnd="@dimen/activity_main_slide_to_cancel_icon_margin_right"
                android:layout_marginRight="@dimen/activity_main_slide_to_cancel_icon_margin_right"
                android:scaleType="center"
                android:src="@drawable/ic_keyboard_arrow_left_black_24dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/label_slide_to_cancel"/>

            <TextView

                android:id="@+id/textView_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:textColor="@color/colorAccent"
                />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"

            android:minHeight="@dimen/activity_main_input_holder_min_height"
            android:orientation="horizontal"
            >

            <EditText
                android:id="@+id/editText_message"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/activity_main_input_margin_left"
                android:layout_marginStart="@dimen/activity_main_input_margin_left"
               android:paddingLeft="16dp"
                android:layout_weight="1"
                android:hint="@string/hint_type_your_message"
                android:paddingBottom="@dimen/activity_main_input_padding_vertical"
                android:paddingTop="@dimen/activity_main_input_padding_vertical"
                android:textSize="@dimen/activity_main_input_text_size"
                android:background="@drawable/chat_input_text"
                android:maxLines="6"
                android:scrollbarStyle="insideInset"
                android:scrollbars="vertical"
                android:layout_gravity="bottom"
                ></EditText>




            <android.support.design.widget.FloatingActionButton
                android:id="@+id/start_record"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/Widget.AppCompat.Button.Borderless.Colored"
                android:src="@drawable/ic_mic_white_24dp"
                android:layout_marginLeft="10dp"
                android:layout_gravity="bottom"
                />



        </LinearLayout>

  </com.example.fcb.insurance.customViews.holdingbutton.HoldingButtonLayout>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/sendMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.Button.Borderless.Colored"
        android:src="@drawable/ic_send_white_24dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:visibility="gone"

        />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_chat_item_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/input_holder"
        android:layout_marginEnd="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginStart="5dp"
        android:clipToPadding="false"
        android:paddingTop="8dp"
        android:visibility="visible"
        app:layoutManager="LinearLayoutManager"
        tools:listitem="@layout/item_chat_list_out"

        />

    </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

在你的onclick事件中

YOUR_EDITTEXT.requestFocus()

答案 1 :(得分:0)

我找到了解决方案。我使用linearLayout包装了recyclerView,并且我禁用了焦点并阻止了后代。

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/input_holder"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:descendantFocusability="blocksDescendants"
    >
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView_chat_item_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginStart="5dp"
    android:clipToPadding="false"
    android:paddingTop="8dp"
    android:visibility="visible"
    app:layoutManager="LinearLayoutManager"
    tools:listitem="@layout/item_chat_list_out"

    />
</LinearLayout>