1. 程式人生 > >EditText常用屬性【三】:EditText選取操作

EditText常用屬性【三】:EditText選取操作

話不多說,直接上碼:

activity_main.xml

[html] view plain copy print?
  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.     <RelativeLayout  
  6.         android:layout_width
    ="fill_parent"  
  7.         android:layout_height="wrap_content" >  
  8.         <EditText  
  9.             android:id="@+id/edit"  
  10.             android:layout_width="fill_parent"  
  11.             android:layout_height="40dp"  
  12.             android:hint="請在這裡輸入文字..."  
  13.             android:inputType="text" />  
  14.         <
    Button  
  15.             android:id="@+id/getAll"  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="40dp"  
  18.             android:layout_below="@+id/edit"  
  19.             android:text="獲取輸入框中的值" />  
  20.         <Button  
  21.             android:id="@+id/getSelect"  
  22.             android:layout_width
    ="fill_parent"  
  23.             android:layout_height="40dp"  
  24.             android:layout_below="@+id/getAll"  
  25.             android:text="獲取被選中的文字" />  
  26.         <Button  
  27.             android:id="@+id/selectAll"  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="40dp"  
  30.             android:layout_below="@+id/getSelect"  
  31.             android:text="全選" />  
  32.         <Button  
  33.             android:id="@+id/selectFrom"  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="40dp"  
  36.             android:layout_below="@+id/selectAll"  
  37.             android:text="從第幾個字元開始選?" />  
  38.         <EditText  
  39.             android:id="@+id/fromNumber"  
  40.             android:layout_width="fill_parent"  
  41.             android:layout_height="40dp"  
  42.             android:layout_below="@+id/selectAll"  
  43.             android:layout_toRightOf="@+id/selectFrom"  
  44.             android:inputType="date"  
  45.             android:hint="在這裡輸入.." />  
  46.         <TextView  
  47.             android:id="@+id/tip"  
  48.             android:layout_width="fill_parent"  
  49.             android:layout_height="wrap_content"  
  50.             android:layout_below="@+id/selectFrom"  
  51.             android:text="提示:焦點必須放在輸入框才能夠選中"  
  52.             />  
  53.     </RelativeLayout>  
  54. </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

[java] view plain copy print?
  1. package com.wirelessqa.edittext;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.text.Editable;  
  5. import android.text.Selection;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.Toast;  
  11. /** 
  12.  * EditText選取操作 
  13.  * From:http://www.csdn.net/blog/wirelessqa 
  14.  * @author bixiaopeng 2013-2-3 下午9:41:57 
  15.  */  
  16. public class MainActivity extends Activity {  
  17.     private EditText edit            = null;  
  18.     private EditText edit_selectFrom = null;  
  19.     private Button   btn_getEdit     = null;  
  20.     private Button   btn_getSelect   = null;  
  21.     private Button   btn_selectAll   = null;  
  22.     private Button   btn_selectFrom  = null;  
  23.     /* (non-Javadoc) 
  24.      * @see android.app.Activity#onCreate(android.os.Bundle) 
  25.      */  
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         // TODO Auto-generated method stub  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_main);  
  31.         edit = (EditText) findViewById(R.id.edit);  
  32.         edit_selectFrom = (EditText) findViewById(R.id.fromNumber);  
  33.         btn_getEdit = (Button) findViewById(R.id.getAll);  
  34.         btn_getSelect = (Button) findViewById(R.id.getSelect);  
  35.         btn_selectAll = (Button) findViewById(R.id.selectAll);  
  36.         btn_selectFrom = (Button) findViewById(R.id.selectFrom);  
  37.         edit.setText("老畢的部落格:http://www.csdn.net/blog/wirelessqa");  
  38.         //監聽獲取輸入框中的所有文字  
  39.         btn_getEdit.setOnClickListener(new OnClickListener() {  
  40.             @Override  
  41.             public void onClick(View v) {  
  42.                 String editText = edit.getText().toString();  
  43.                 Toast.makeText(MainActivity.this, editText, Toast.LENGTH_LONG).show();  
  44.             }  
  45.         });  
  46.         //監聽獲取選中的文字  
  47.         btn_getSelect.setOnClickListener(new OnClickListener() {  
  48.             @Override  
  49.             public void onClick(View v) {  
  50.                 int startSelect = edit.getSelectionStart();  
  51.                 int endSelect = edit.getSelectionEnd();  
  52.                 String selectText = edit.getText().subSequence(startSelect, endSelect).toString();  
  53.                 Toast.makeText(MainActivity.this, selectText, Toast.LENGTH_LONG).show();  
  54.             }  
  55.         });  
  56.         //全選  
  57.         btn_selectAll.setOnClickListener(new OnClickListener() {  
  58.             @Override  
  59.             public void onClick(View v) {  
  60.                 setEditFocus(edit);  
  61.                 edit.selectAll();    
  62.             }  
  63.         });  
  64.         //從第幾個字元開始選擇  
  65.         btn_selectFrom.setOnClickListener(new OnClickListener() {  
  66.             @Override  
  67.             public void onClick(View v) {  
  68.                 //從輸入框中獲取值  
  69.                 int fromNumber = 0;  
  70.                 try {  
  71.                     fromNumber = Integer.valueOf(edit_selectFrom.getText().toString());  
  72.                 } catch (Exception e) {  
  73.                     e.printStackTrace();  
  74.                     fromNumber = 0;  
  75.                     Toast.makeText(MainActivity.this"請輸入大於0的數字", Toast.LENGTH_SHORT).show();  
  76.                 }  
  77.                 int length = edit.getText().length()-1;//輸入框中文字的長度  
  78.                 if(fromNumber !=0 && fromNumber<length){  
  79.                     Editable editable = edit.getText();  
  80.                     setEditFocus(edit);  
  81.                     Selection.setSelection(editable,fromNumber,editable.length());  
  82.                 }else{  
  83.                     Toast.makeText(MainActivity.this"輸入的數字要小於"+length, Toast.LENGTH_SHORT).show();  
  84.                 }  
  85.             }  
  86.         });  
  87.     }  
  88.     /** 
  89.      * 將焦點放在輸入框中 
  90.      * 如果想要選中輸入框中的文字必須要將焦點放在輸入框中 
  91.      * 如果想要焦點在輸入框中必須設定下面三個方法 
  92.      * @param editText 
  93.      */  
  94.     private void setEditFocus(EditText editText){  
  95.         editText.setFocusable(true);  
  96.         editText.setFocusableInTouchMode(true);  
  97.         editText.requestFocus();  
  98.     }  
  99. }  
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,謝謝!^^