用EditText 实现 textView 长按复制效果

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_35192370/article/details/79577388

首先 EditText 是集成 TextView 的

我们直接将 EditText 设置为不可编辑来实现

<EditText
                    android:id="@+id/activity_detail_activity_des_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:editable="false"
                    android:lineSpacingExtra="@dimen/dimen_12_dip"
                    android:paddingBottom="@dimen/dimen_30_dip"
                    android:paddingLeft="@dimen/dimen_30_dip"
                    android:paddingRight="@dimen/dimen_30_dip"
                    android:paddingTop="@dimen/dimen_16_dip"
                    android:text="活动描述"
                    android:textColor="@color/c39414d"
                    android:textCursorDrawable="@null"
                    android:textIsSelectable="true"
                    android:textSize="@dimen/dimen_30_dip" />

其中重点为:

android:background=”@null”//背景设为null,去掉editText下划线
android:textCursorDrawable=”@null”//游标设为null,去掉游标
android:editable=”false”//不可编辑

android:textIsSelectable=”true”//文字内容可选

有时候可能会遇到 无法复制的问题是因为:

EditText没有获取到焦点

有可能为父控件将焦点截获了

其父控件 会有一个方法

descendantFocusability

    beforeDescendants:viewgroup会优先其子类控件而获取到焦点

    afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

    blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

应该可以解决

猜你喜欢

转载自blog.csdn.net/baidu_35192370/article/details/79577388