1. 程式人生 > >android editText 字元長度限制

android editText 字元長度限制

方法一:

在 xml 檔案中設定文字編輯框屬性作字元數限制

如:android:maxLength="10" 即限制最大輸入字元個數為10

方法二:

在程式碼中使用InputFilter 進行過濾

//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大輸入字元數為20

  1. publicclass TextEditActivity extends Activity { 
  2.     /** Called when the activity is first created. */
  3.     @Override
  4.     publicvoid onCreate(Bundle savedInstanceState) { 
  5.         super.onCreate(savedInstanceState); 
  6.         setContentView(R.layout.main); 
  7.         EditText editText = (EditText)findViewById(R.id.entry); 
  8.         editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 
  9.     } 
public class TextEditActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        EditText editText = (EditText)findViewById(R.id.entry);
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
    }
}

方法三:

利用 TextWatcher 進行監聽

  1. package cie.textEdit; 
  2. import android.text.Editable; 
  3. import android.text.Selection; 
  4. import android.text.TextWatcher; 
  5. import android.widget.EditText; 
  6. /*
  7. * 監聽輸入內容是否超出最大長度,並設定游標位置
  8. * */
  9. publicclass MaxLengthWatcher implements TextWatcher { 
  10.     privateint maxLen = 0
  11.     private
     EditText editText = null
  12.     public MaxLengthWatcher(int maxLen, EditText editText) { 
  13.         this.maxLen = maxLen; 
  14.         this.editText = editText; 
  15.     } 
  16.     publicvoid afterTextChanged(Editable arg0) { 
  17.         // TODO Auto-generated method stub
  18.     } 
  19.     publicvoid beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
  20.             int arg3) { 
  21.         // TODO Auto-generated method stub
  22.     } 
  23.     publicvoid onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
  24.         // TODO Auto-generated method stub
  25.         Editable editable = editText.getText(); 
  26.         int len = editable.length(); 
  27.         if(len > maxLen) 
  28.         { 
  29.             int selEndIndex = Selection.getSelectionEnd(editable); 
  30.             String str = editable.toString(); 
  31.             //擷取新字串
  32.             String newStr = str.substring(0,maxLen); 
  33.             editText.setText(newStr); 
  34.             editable = editText.getText(); 
  35.             //新字串的長度
  36.             int newLen = editable.length(); 
  37.             //舊游標位置超過字串長度
  38.             if(selEndIndex > newLen) 
  39.             { 
  40.                 selEndIndex = editable.length(); 
  41.             } 
  42.             //設定新游標所在的位置
  43.             Selection.setSelection(editable, selEndIndex); 
  44.         } 
  45.     } 
package cie.textEdit;

import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;

/*
 * 監聽輸入內容是否超出最大長度,並設定游標位置
 * */
public class MaxLengthWatcher implements TextWatcher {

	private int maxLen = 0;
	private EditText editText = null;
	
	
	public MaxLengthWatcher(int maxLen, EditText editText) {
		this.maxLen = maxLen;
		this.editText = editText;
	}

	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		
	}

	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub
		
	}

	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		Editable editable = editText.getText();
		int len = editable.length();
		
		if(len > maxLen)
		{
			int selEndIndex = Selection.getSelectionEnd(editable);
			String str = editable.toString();
			//擷取新字串
			String newStr = str.substring(0,maxLen);
			editText.setText(newStr);
			editable = editText.getText();
			
			//新字串的長度
			int newLen = editable.length();
			//舊游標位置超過字串長度
			if(selEndIndex > newLen)
			{
				selEndIndex = editable.length();
			}
			//設定新游標所在的位置
			Selection.setSelection(editable, selEndIndex);
			
		}
	}

}

對應的 activity 部分的呼叫為:

  1. package cie.textEdit; 
  2. import android.app.Activity; 
  3. import android.os.Bundle; 
  4. import android.text.InputFilter; 
  5. import android.widget.EditText; 
  6. publicclass TextEditActivity extends Activity { 
  7.     /** Called when the activity is first created. */
  8.     @Override
  9.     publicvoid onCreate(Bundle savedInstanceState) { 
  10.         super.onCreate(savedInstanceState); 
  11.         setContentView(R.layout.main); 
  12.         EditText editText = (EditText) findViewById(R.id.entry); 
  13.         editText.addTextChangedListener(new MaxLengthWatcher(10, editText)); 
  14.     } 
package cie.textEdit;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.widget.EditText;

public class TextEditActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		EditText editText = (EditText) findViewById(R.id.entry);
		editText.addTextChangedListener(new MaxLengthWatcher(10, editText));

    }
}

限制輸入字元數為10個

main.xml 檔案

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent">
  5.     <TextView
  6.         android:id="@+id/label"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:text="Type here:"/>
  10.     <EditText
  11.         android:id="@+id/entry"
  12.         android:singleLine="true"
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="wrap_content"
  15.         android:background="@android:drawable/editbox_background"
  16.         android:layout_below="@id/label"/>
  17.     <Button
  18.         android:id="@+id/ok"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:layout_below="@id/entry"
  22.         android:layout_alignParentRight="true"
  23.         android:layout_marginLeft="10dip"
  24.         android:text="OK"/>
  25.     <Button
  26.         android:layout_width="wrap_content"
  27.         android:layout_height="wrap_content"
  28.         android:layout_toLeftOf="@id/ok"
  29.         android:layout_alignTop="@id/ok"
  30.         android:text="Cancel"/>
  31. </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type here:"/>
    <EditText
        android:id="@+id/entry"
        android:singleLine="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="OK" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel" />
</RelativeLayout>

效果為輸入了10個字元後,游標停在末尾