android editText 字元長度限制
阿新 • • 發佈:2019-01-23
方法一:
在 xml 檔案中設定文字編輯框屬性作字元數限制
如:android:maxLength="10" 即限制最大輸入字元個數為10
方法二:
在程式碼中使用InputFilter 進行過濾
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大輸入字元數為20
- publicclass TextEditActivity extends Activity {
- /** Called when the activity is first created. */
-
@Override
- publicvoid 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)});
- }
- }
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 進行監聽
- package cie.textEdit;
- import android.text.Editable;
- import android.text.Selection;
- import android.text.TextWatcher;
- import android.widget.EditText;
- /*
- * 監聽輸入內容是否超出最大長度,並設定游標位置
- * */
- publicclass MaxLengthWatcher implements TextWatcher {
- privateint maxLen = 0;
-
private
- public MaxLengthWatcher(int maxLen, EditText editText) {
- this.maxLen = maxLen;
- this.editText = editText;
- }
- publicvoid afterTextChanged(Editable arg0) {
- // TODO Auto-generated method stub
- }
- publicvoid beforeTextChanged(CharSequence arg0, int arg1, int arg2,
- int arg3) {
- // TODO Auto-generated method stub
- }
- publicvoid 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);
- }
- }
- }
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 部分的呼叫為:
- package cie.textEdit;
- import android.app.Activity;
- import android.os.Bundle;
- import android.text.InputFilter;
- import android.widget.EditText;
- publicclass TextEditActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- EditText editText = (EditText) findViewById(R.id.entry);
- editText.addTextChangedListener(new MaxLengthWatcher(10, editText));
- }
- }
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 檔案
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns: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>
<?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個字元後,游標停在末尾