Recyclerview 中包含 webView 和 EditText 导致 编辑框无法输入的问题

不知道怎么就遇到了一些奇葩的需求,掉进了一写奇葩的坑,现在的话就是在记录一下脱坑历程,最近做的一个页面,分为上中下三个部分,上面是原生的布局,中间全是webView 下面又是一个原生布局。一开始是打算是用NestedScrollView 来写的,结果做出来发现,webView的高度无法自适应,而且连滑动也是冲突了。没有办法,只有使用RecyClerview ,把上面的部分和webView 写在一个布局里,作为Rv的第一个Viewholder,最底部的原生页面作为第二个ViewHolder.
写出来之后发现webView 抢占了输入框的焦点事件,导致编辑框无法调起输入法输入。

  • Activity 的页面布局如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   android:descendantFocusability="blocksDescendants"
    tools:context=".activity.ProDetalsH5Activity">

    <include
        android:id="@+id/include"
        layout="@layout/layout_title" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/webRecy"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
  • recyclerview 第一个viewHolder的布局如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">


        <TextView
            android:id="@+id/proNames"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_33"
            android:layout_marginTop="@dimen/dp_29"
            android:text="日常保洁"
            android:textColor="#ff333333"
            android:textSize="@dimen/sp_32"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/view"
            android:layout_width="@dimen/dp_58"
            android:layout_height="@dimen/dp_5"
            android:layout_marginTop="@dimen/dp_19"
            android:background="#E4922A"
            app:layout_constraintLeft_toLeftOf="@id/proNames"
            app:layout_constraintTop_toBottomOf="@id/proNames" />

        <TextView
            android:id="@+id/proInfo"
            android:layout_width="@dimen/dp_303"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_27"
            android:lineSpacingExtra="@dimen/dp_5"
            android:text="提供家庭室内表面清洁、物品归纳整理和高温
消费服务。"
            android:textColor="#ff999999"
            android:textSize="15sp"
            app:layout_constraintLeft_toLeftOf="@id/view"
            app:layout_constraintTop_toBottomOf="@id/view" />

        <View
            android:id="@+id/view1"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_1"
            android:layout_marginTop="@dimen/dp_29"
            android:background="#EEEEEE"
            app:layout_constraintLeft_toLeftOf="@id/proInfo"
            app:layout_constraintTop_toBottomOf="@id/proInfo" />

        <ImageView
            android:id="@+id/view2"
            android:layout_width="@dimen/dp_19"
            android:layout_height="@dimen/dp_19"
            android:layout_marginLeft="@dimen/dp_74"
            android:layout_marginTop="@dimen/dp_24"
            android:src="@drawable/people_iocn"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/view1" />

        <TextView
            android:id="@+id/canPeople"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_10"
            android:text="在线可预约服务人员  89 人"
            android:textColor="#ffd74e3f"
            android:textSize="@dimen/sp_15"
            app:layout_constraintBottom_toBottomOf="@id/view2"
            app:layout_constraintLeft_toRightOf="@id/view2"
            app:layout_constraintTop_toTopOf="@id/view2" />


        <LinearLayout
            android:id="@+id/layoutTime"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_43"
            android:layout_marginTop="@dimen/dp_27"
            android:background="@drawable/shape_rounded_rectangle_color_gray_stork"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/canPeople">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/sp_15"
                android:text="预约时间:"
                android:textColor="#ff626262"
                android:textSize="@dimen/sp_14" />


            <TextView
                android:id="@+id/inputTime"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center|left"
                android:hint="年/月/日 --:--"
                android:paddingLeft="@dimen/dp_15"
                android:textColor="@color/rv_item_tv" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutAddress"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_43"
            android:layout_marginTop="@dimen/dp_12"
            android:background="@drawable/shape_rounded_rectangle_color_gray_stork"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layoutTime">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/sp_15"
                android:text="服务地址:"
                android:textColor="#ff626262"
                android:textSize="@dimen/sp_14" />


            <TextView
                android:id="@+id/inputAddress"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:ellipsize="end"
                android:gravity="center|left"
                android:hint="请填写您的楼号-门牌号"
                android:maxLength="20"
                android:paddingLeft="@dimen/dp_15"
                android:singleLine="true"
                android:textColor="@color/rv_item_tv" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutPhone"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_43"
            android:layout_marginTop="@dimen/dp_12"
            android:background="@drawable/shape_rounded_rectangle_color_gray_stork"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layoutAddress">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/sp_15"
                android:text="联系电话:"
                android:textColor="#ff626262"
                android:textSize="@dimen/sp_14" />


            <EditText
                android:id="@+id/inputPhone"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@null"
                android:cursorVisible="true"
                android:maxLength="11"
                android:gravity="center_vertical"
                android:hint="请填写您的联系方式"
                android:inputType="number"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:paddingLeft="@dimen/dp_15"
                android:textColor="@color/rv_item_tv"
                android:textCursorDrawable="@drawable/color_cursor"
                android:textSize="@dimen/sp_14" />


        </LinearLayout>

        <TextView
            android:id="@+id/callPeople"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_43"
            android:layout_marginTop="@dimen/dp_19"
            android:background="@drawable/confirm_roundbtn_click_selector"
            android:clickable="true"
            android:gravity="center"
            android:text="呼叫服务"
            android:textColor="@color/white"
            android:textSize="@dimen/sp_15"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layoutPhone" />

        <View
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_1"
            android:layout_marginTop="@dimen/dp_29"
            android:background="#EEEEEE"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/callPeople" />

    </android.support.constraint.ConstraintLayout>


    <!--<include-->
    <!--layout="@layout/activity_pro_details_h5_in_put_content"-->
    <!--android:visibility="gone" />-->


    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
       android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_height="wrap_content"
        tools:ignore="WebViewLayout" />


</LinearLayout>

其实我是认为这样写是没问题的,我给EditText 设置了获取焦点的属性,给webView 设置了禁止获取焦点的属性,但是运行结果是狠狠打脸了,编辑框是无法输入的,后来才发现,罪魁祸首是这一个属性

   android:descendantFocusability="blocksDescendants"

我居然不知道什么时候给我的Activity布局上加入了这个属性,对于这个属性,有的一说

  beforeDescendants:viewgroup会优先其子类控件而获取到焦点
  afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
  blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

所以当我把这个属性去掉后,editText 就能正常输入了。

另外,关于Koltin 中给editText 赋初值的方法

习惯了java的setText 方法后,使用Kotlin 已经没有这种写法了,ed.text="123456799"结果这种写法会报一个类型不匹配的错误,现在也说一下解决的办法。那么正确的写法就是:

  mHolder.input_phone?.text=
                        if (TextUtils.isEmpty(mPhone)) Editable.Factory.getInstance().newEditable("请输入手机号")
                        else Editable.Factory.getInstance().newEditable(mPhone)

对的,就是要把文字转成EditAble 类型的才可以正常设置。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,701评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,649评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,037评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,994评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,018评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,796评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,481评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,370评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,868评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,014评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,153评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,832评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,494评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,039评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,437评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,131评论 2 356

推荐阅读更多精彩内容