Android selects TextView text

Source: Internet
Author: User

I read a post on the Internet to learn how to freely select TextView text. I want to analyze the source code, but the network speed is not good. I can't open it online after I download the source code.

I will post the implementation code, basically the code on that post: after reading the source code, I will add it.

package com.example.view;import android.content.Context;import;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);}@Overrideprotected boolean getDefaultEditable() {return false;} @Overridepublic 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 ;  } }

Layout File

<RelativeLayout xmlns:android=""    xmlns: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>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.