(获取选中的光标起始位置)EditText常用属性【三】:EditText选取操作

转自:http://blog.csdn.net/wirelessqa/article/details/8567702

话不多说,直接上码:

activity_main.xml

[html] view plaincopy
 
  1. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <RelativeLayout  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content" >  
  9.   
  10.         <EditText  
  11.             android:id="@+id/edit"  
  12.             android:layout_width="fill_parent"  
  13.             android:layout_height="40dp"  
  14.             android:hint="请在这里输入文本..."  
  15.             android:inputType="text" />  
  16.   
  17.         <Button  
  18.             android:id="@+id/getAll"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="40dp"  
  21.             android:layout_below="@+id/edit"  
  22.             android:text="获取输入框中的值" />  
  23.   
  24.         <Button  
  25.             android:id="@+id/getSelect"  
  26.             android:layout_width="fill_parent"  
  27.             android:layout_height="40dp"  
  28.             android:layout_below="@+id/getAll"  
  29.             android:text="获取被选中的文本" />  
  30.   
  31.         <Button  
  32.             android:id="@+id/selectAll"  
  33.             android:layout_width="fill_parent"  
  34.             android:layout_height="40dp"  
  35.             android:layout_below="@+id/getSelect"  
  36.             android:text="全选" />  
  37.   
  38.         <Button  
  39.             android:id="@+id/selectFrom"  
  40.             android:layout_width="wrap_content"  
  41.             android:layout_height="40dp"  
  42.             android:layout_below="@+id/selectAll"  
  43.             android:text="从第几个字符开始选?" />  
  44.   
  45.         <EditText  
  46.             android:id="@+id/fromNumber"  
  47.             android:layout_width="fill_parent"  
  48.             android:layout_height="40dp"  
  49.             android:layout_below="@+id/selectAll"  
  50.             android:layout_toRightOf="@+id/selectFrom"  
  51.             android:inputType="date"  
  52.             android:hint="在这里输入.." />  
  53.           
  54.         <TextView  
  55.             android:id="@+id/tip"  
  56.             android:layout_width="fill_parent"  
  57.             android:layout_height="wrap_content"  
  58.             android:layout_below="@+id/selectFrom"  
  59.             android:text="提示:焦点必须放在输入框才能够选中"  
  60.             />  
  61.     </RelativeLayout>  
  62.   
  63. </ScrollView>  

MainActivity.java

[java] view plaincopy
 
  1. package com.wirelessqa.edittext;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.text.Editable;  
  6. import android.text.Selection;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.Toast;  
  12.   
  13. /** 
  14.  * EditText选取操作 
  15.  * From:http://www.csdn.net/blog/wirelessqa 
  16.  * @author bixiaopeng 2013-2-3 下午9:41:57 
  17.  */  
  18. public class MainActivity extends Activity {  
  19.   
  20.     private EditText edit            = null;  
  21.     private EditText edit_selectFrom = null;  
  22.     private Button   btn_getEdit     = null;  
  23.     private Button   btn_getSelect   = null;  
  24.     private Button   btn_selectAll   = null;  
  25.     private Button   btn_selectFrom  = null;  
  26.   
  27.     /* (non-Javadoc) 
  28.      * @see android.app.Activity#onCreate(android.os.Bundle) 
  29.      */  
  30.     @Override  
  31.     protected void onCreate(Bundle savedInstanceState) {  
  32.         // TODO Auto-generated method stub  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.activity_main);  
  35.   
  36.         edit = (EditText) findViewById(R.id.edit);  
  37.         edit_selectFrom = (EditText) findViewById(R.id.fromNumber);  
  38.         btn_getEdit = (Button) findViewById(R.id.getAll);  
  39.         btn_getSelect = (Button) findViewById(R.id.getSelect);  
  40.         btn_selectAll = (Button) findViewById(R.id.selectAll);  
  41.         btn_selectFrom = (Button) findViewById(R.id.selectFrom);  
  42.           
  43.         edit.setText("老毕的博客:http://www.csdn.net/blog/wirelessqa");  
  44.         //监听获取输入框中的所有文本  
  45.         btn_getEdit.setOnClickListener(new OnClickListener() {  
  46.               
  47.             @Override  
  48.             public void onClick(View v) {  
  49.                 String editText = edit.getText().toString();  
  50.                 Toast.makeText(MainActivity.this, editText, Toast.LENGTH_LONG).show();  
  51.             }  
  52.         });  
  53.         //监听获取选中的文本  
  54.         btn_getSelect.setOnClickListener(new OnClickListener() {  
  55.               
  56.             @Override  
  57.             public void onClick(View v) {  
  58.                 int startSelect = edit.getSelectionStart();  
  59.                 int endSelect = edit.getSelectionEnd();  
  60.                 String selectText = edit.getText().subSequence(startSelect, endSelect).toString();  
  61.                 Toast.makeText(MainActivity.this, selectText, Toast.LENGTH_LONG).show();  
  62.             }  
  63.         });  
  64.         //全选  
  65.         btn_selectAll.setOnClickListener(new OnClickListener() {  
  66.               
  67.             @Override  
  68.             public void onClick(View v) {  
  69.                 setEditFocus(edit);  
  70.                 edit.selectAll();    
  71.                   
  72.             }  
  73.         });  
  74.         //从第几个字符开始选择  
  75.         btn_selectFrom.setOnClickListener(new OnClickListener() {  
  76.               
  77.             @Override  
  78.             public void onClick(View v) {  
  79.                 //从输入框中获取值  
  80.                 int fromNumber = 0;  
  81.                 try {  
  82.                     fromNumber = Integer.valueOf(edit_selectFrom.getText().toString());  
  83.                 } catch (Exception e) {  
  84.                     e.printStackTrace();  
  85.                     fromNumber = 0;  
  86.                     Toast.makeText(MainActivity.this, "请输入大于0的数字", Toast.LENGTH_SHORT).show();  
  87.                 }  
  88.                 int length = edit.getText().length()-1;//输入框中文本的长度  
  89.                 if(fromNumber !=0 && fromNumber<length){  
  90.                     Editable editable = edit.getText();  
  91.                     setEditFocus(edit);  
  92.                     Selection.setSelection(editable,fromNumber,editable.length());  
  93.                 }else{  
  94.                     Toast.makeText(MainActivity.this, "输入的数字要小于"+length, Toast.LENGTH_SHORT).show();  
  95.                 }  
  96.                  
  97.             }  
  98.         });  
  99.           
  100.     }  
  101.       
  102.     /** 
  103.      * 将焦点放在输入框中 
  104.      * 如果想要选中输入框中的文本必须要将焦点放在输入框中 
  105.      * 如果想要焦点在输入框中必须设置下面三个方法 
  106.      * @param editText 
  107.      */  
  108.     private void setEditFocus(EditText editText){  
  109.         editText.setFocusable(true);  
  110.         editText.setFocusableInTouchMode(true);  
  111.         editText.requestFocus();  
  112.     }  
  113.   
  114. }  


本文链接:http://blog.csdn.net/wirelessqa/article/details/8567702

转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:http://blog.csdn.net/wirelessqa,谢谢!^^

原文地址:https://www.cnblogs.com/x_wukong/p/4450424.html