在网上看了一个帖子如何自由选择TextView的文字的,感觉还不错。想分析一下源码,但是网速不好,等下载下了源码再看吧,在线的打不开。
我就把实现的代码贴出来吧,基本上就那个帖子上的代码:原理看完源码再补上
package com.example.view;
import android.content.Context;
import android.graphics.Color;
import android.text.Layout;
import android.text.Selection;
import android.text.method.MovementMethod;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.EditText;
public class SelectedTextView extends EditText{
private int off;
public SelectedTextView(Context context) {
super(context);
init();
}
public SelectedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public SelectedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
setGravity(Gravity.TOP);
this.setBackgroundColor(Color.WHITE);
}
@Override
protected boolean getDefaultEditable() {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
Layout layout = getLayout();
int line = 0 ;
switch (action) {
case MotionEvent.ACTION_DOWN:
line = layout.getLineForVertical(getScrollY()+ (int )event.getY());
off = layout.getOffsetForHorizontal(line, (int )event.getX());
Selection.setSelection(getEditableText(), off);
break ;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
line = layout.getLineForVertical(getScrollY()+(int )event.getY());
int curOff = layout.getOffsetForHorizontal(line, ( int )event.getX());
Selection.setSelection(getEditableText(), off, curOff);
break ;
}
return true ;
}
}
布局文件
<RelativeLayout 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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<com.example.view.SelectedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcdefghijklmnopqrstuvwxyzxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
/>
</RelativeLayout>
分享到:
相关推荐
Android实现TextView文字滚动
android实现TextView文字部分渐变色;LinearGradientFontSpan SpannableString
Android textview 文字中间加上图片
旋转TextView文字显示方向,可参考学习一下。
有时候看到原型图上要求图片和文字对齐,但是直接使用TextView的话总是有一段空白,这里直接继承TextView重写onDraw方法去掉上下边距,但是简单粗暴,导致只能显示文字,没有TextView其他的功能了。
android textview 文字排版 换行
Android自定义TextView实现动态的文字效果
主要介绍了Android实现在TextView文字过长时省略部分或滚动显示的方法,结合实例形式分析了Android中TextView控件文字显示及滚动效果相关操作技巧,需要的朋友可以参考下
通过textview设置状态选择
Android SpannableString实现TextView不同大小文字居中显示。
Android流式TextView,类似文字标签。
Android 为textView增加边框效果,这又是一个初级的Android文字布局示例,今天是为TextView文字框增加一个外边框,可实现在画布上画边框的功能,在填充方面,你还可使用不透明的9.png和透明的9.png,带边框的...
Android TextView 个别文字字体颜色格式
解决TextView的排版问题-自动换行
解决Android中原生TextView显示中英文特殊字符等末尾参差不齐问题
本文实例讲述了Android TextView实现垂直滚动效果的方法。分享给大家供大家参考,具体如下: 在TextView中,如果文本很长,可能需要实现垂直滚动显示文本的效果。这里需要在XML布局文件中为TextView设置如下几个属性...
在Android中用textview实现简单的小说分页,字体可调节,未对字符串进行任何操作,分段和控制格式自行对字符串操作
Android textview 文字中间加上图片.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
这个代码主要以Android中的TextView发生变化,而凡是订阅了此类消息的实体类对象均能够打印出日志。例子很简单,起抛砖引玉,希望大家能够理解观察者模式在Android中的应用
一个自定义的android的翻转textview,效果很不错的