您的位置:首页 > 移动开发 > Android开发

Android UI实现单行文本水平触摸滑动效果

2016-10-24 11:33 405 查看

本文实例为大家分享了单行文本水平触摸滑动效果,通过EditText实现TextView单行长文本水平滑动效果。
下一篇再为大家介绍 多行文本折叠展开效果,自定义布局View实现多行文本折叠和展开。

1.初衷

  最近做应用的时候有用到TextView单行长文本,当文本内容过长时候又想实现触摸水平滑动效果。网上找了很多,都没有看到有效解决方案。
  其中,看到最常见的也是最笨拙滴采用重写TextView并继承实现touch 和 Gesture手势。个人觉得很麻烦。

  后来经提醒发现了其实最简单的方案:

  直接使用 EditText 就好了。 因为EditText需要编辑和移动光标的缘故,使得它是可以水平滑动的。因此我们只需要设置其为透明背景,并且不可以获得焦点。

效果图:

2.具体实现

  定义EditText,并设置背景透明,不能获得焦点。

android:background=”@android:color/transparent”
android:focusable=”false”

(注:不能使用 editable=“false”,因为这样就不能编辑滑动。而通过使用 focusable=”false” 同样不可编辑同时可以滑动)

3.补充

  补充一点,要隐藏光标,只用设置

android:cursorVisible=”false”//隐藏

setCursorVisible(false);

代码示例:

<EditText
android:id="@+id/tt"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="15dip"
android:background="@android:color/transparent"
android:focusable="false"
android:singleLine="true"
android:cursorVisible="false"
android:text="简易实现 TextView单行文本水平触摸滑动效果。简易实现 TextView单行文本水平触摸滑动效果。简易实现 TextView单行文本水平触摸滑动效果"
android:textColor="#000"
android:textSize="20dip" />

附上下载地址:http://xiazai.jb51.net/201610/yuanma/Androidtouchmove(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android UI 触摸 滑动