EditText常用属性【三】:EditText选取操作

话不多说,直接上码:

activity_main.xml


  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>  
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/edit"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:hint="请在这里输入文本..."
            android:inputType="text" />

        <Button
            android:id="@+id/getAll"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/edit"
            android:text="获取输入框中的值" />

        <Button
            android:id="@+id/getSelect"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/getAll"
            android:text="获取被选中的文本" />

        <Button
            android:id="@+id/selectAll"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/getSelect"
            android:text="全选" />

        <Button
            android:id="@+id/selectFrom"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_below="@+id/selectAll"
            android:text="从第几个字符开始选?" />

        <EditText
            android:id="@+id/fromNumber"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/selectAll"
            android:layout_toRightOf="@+id/selectFrom"
           	android:inputType="date"
            android:hint="在这里输入.." />
        
        <TextView
            android:id="@+id/tip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/selectFrom"
            android:text="提示:焦点必须放在输入框才能够选中"
            />
    </RelativeLayout>

</ScrollView>

MainActivity.java


  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. }  
package com.wirelessqa.edittext;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * EditText选取操作
 * From:http://www.csdn.net/blog/wirelessqa
 * @author bixiaopeng 2013-2-3 下午9:41:57
 */
public class MainActivity extends Activity {

    private EditText edit            = null;
    private EditText edit_selectFrom = null;
    private Button   btn_getEdit     = null;
    private Button   btn_getSelect   = null;
    private Button   btn_selectAll   = null;
    private Button   btn_selectFrom  = null;

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edit = (EditText) findViewById(R.id.edit);
        edit_selectFrom = (EditText) findViewById(R.id.fromNumber);
        btn_getEdit = (Button) findViewById(R.id.getAll);
        btn_getSelect = (Button) findViewById(R.id.getSelect);
        btn_selectAll = (Button) findViewById(R.id.selectAll);
        btn_selectFrom = (Button) findViewById(R.id.selectFrom);
        
        edit.setText("老毕的博客:http://www.csdn.net/blog/wirelessqa");
        //监听获取输入框中的所有文本
        btn_getEdit.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                String editText = edit.getText().toString();
                Toast.makeText(MainActivity.this, editText, Toast.LENGTH_LONG).show();
            }
        });
        //监听获取选中的文本
        btn_getSelect.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                int startSelect = edit.getSelectionStart();
                int endSelect = edit.getSelectionEnd();
                String selectText = edit.getText().subSequence(startSelect, endSelect).toString();
                Toast.makeText(MainActivity.this, selectText, Toast.LENGTH_LONG).show();
            }
        });
        //全选
        btn_selectAll.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                setEditFocus(edit);
                edit.selectAll();  
                
            }
        });
        //从第几个字符开始选择
        btn_selectFrom.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //从输入框中获取值
                int fromNumber = 0;
                try {
                    fromNumber = Integer.valueOf(edit_selectFrom.getText().toString());
                } catch (Exception e) {
                    e.printStackTrace();
                    fromNumber = 0;
                    Toast.makeText(MainActivity.this, "请输入大于0的数字", Toast.LENGTH_SHORT).show();
                }
                int length = edit.getText().length()-1;//输入框中文本的长度
                if(fromNumber !=0 && fromNumber<length){
                    Editable editable = edit.getText();
                    setEditFocus(edit);
                    Selection.setSelection(editable,fromNumber,editable.length());
                }else{
                    Toast.makeText(MainActivity.this, "输入的数字要小于"+length, Toast.LENGTH_SHORT).show();
                }
               
            }
        });
        
    }
    
    /**
     * 将焦点放在输入框中
     * 如果想要选中输入框中的文本必须要将焦点放在输入框中
     * 如果想要焦点在输入框中必须设置下面三个方法
     * @param editText
     */
    private void setEditFocus(EditText editText){
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
    }

}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值