可复制内容的TextView、EditText

有时候我们做app的时候,想要让用户可以复制一些文本内容。此时,网上有一些自定义TextView可以实现该功能。但是,不免有点麻烦。其实,直接设置xml属性就可以实现该功能。

  • xml代码
  • 效果图
  • 属性说明

xml代码:

<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:gravity="center"    android:orientation="vertical"    tools:context="com.xu.tvBeCopy.MainActivity" >    <EditText        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@null"        android:cursorVisible="false"        android:inputType="none"        android:text="朋友,你怎么这么帅啊!!!!"        android:textColor="#000000"        android:textIsSelectable="true" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="我是TextView美女!!!"        android:textColor="#000000"        android:textIsSelectable="true" />LinearLayout>

效果图:

EditText:

TextView:

属性说明

android:textIsSelectable=”true”属性是在API 11之后才有的。如果设置成true,长按后就可以进行文字的复制粘贴。
EditText的android:inputType=”none”是为了不能让用户输入内容。
EditText的android:cursorVisible=”false”是为了隐藏掉输入框的光标。
EditText的android:background=”@null”是为了设置背景为空,隐藏掉输入框的下划线。
但是,注意一点,用了这个属性就不能用剪切功能。

更多相关文章

  1. Instant Run详解
  2. Android(安卓)之 API级别
  3. Android样式研究
  4. 【Android源码】Android(安卓)UI 之一步步教你自定义控件
  5. Android(安卓)之 -- DRM(转)
  6. android:windowSoftInputMode 属性介绍
  7. 2018-03-10—Android六大布局之LinearLayout和RelativeLayout
  8. Android(安卓)SDK2.2离线安装
  9. Android(安卓)中自定义属性(attr.xml,TypedArray)的使用!

随机推荐

  1. Android(安卓)Socket编程
  2. Android(安卓)实现开机自启动 Service
  3. Android(安卓)ble setCharactersticNotif
  4. android WebView结合javascript相互调用
  5. Android(安卓)使用 Gmail 来发送邮件
  6. Android(安卓)Studio打包时出现transform
  7. Android自定义对话框(Dialog)位置,大小
  8. Android(安卓)animation - 文字旋转示例
  9. android launch 初探
  10. Android适配底部虚拟键盘遮挡布局的解决