Android EditText輸入最大長度限制如何給使用者以友好的提示,androidedittext
我們知道EditText有個屬性android:maxLength="xxx" ,可以設定EditText的最大長度。
也可以用如下代碼設定長度,editText.setFilters( new InputFilter[]{ new InputFilter.LengthFilter( 50 )});
然後我寫個小樣本,xml布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="請輸入:" /> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLength="15" /> </LinearLayout>View Code
設定最大長度為15
MainActivity 如下:
package com.example.edittext;import android.app.Activity;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.util.Log;import android.view.Gravity;import android.widget.EditText;import android.widget.Toast; public class MainActivity extends Activity { private EditText editText = null; private Toast toast = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.editText); editText.addTextChangedListener(textWatcher); } private TextWatcher textWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { //TODO } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { //TODO } @Override public void afterTextChanged(Editable s) { Log.d("TAG","afterTextChanged "+"str="+s.toString()); int len = s.toString().length(); if(len>=15){ toast = Toast.makeText(getApplicationContext(), "字元不能超過15個", 1000); toast.setGravity(Gravity.TOP, 0, 235); toast.show(); } } }; }
我們給EditText加上textChanged監聽器,並在afterTextChanged中對已經輸入文本的長度進行判斷,當長度大於或者等於15時
我們給出提示字元不能超過15個,如、
但是,問題來了。提示完字元不能超過15個之後,假設我們再進行輸入操作,這時候就不再提示了。
假設使用者沒有注意到彈的toast,而超過15個字元後使用者還在輸入,但是文字框卻沒有變化,造成體驗就不好了。
我們希望的是超過15個字元後使用者如果繼續輸入,我們依舊每次都提示他字元不能超過15個。
我們修改代碼如下,
package com.example.edittext;import android.app.Activity;import android.os.Bundle;import android.text.InputFilter;import android.text.Spanned;import android.view.Gravity;import android.widget.EditText;import android.widget.Toast; public class MainActivity extends Activity { private EditText editText = null; private Toast toast = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.editText); editText.setFilters(new InputFilter[]{new MaxTextLengthFilter(16)}); } class MaxTextLengthFilter implements InputFilter{ private int mMaxLength; public MaxTextLengthFilter(int max){ mMaxLength = max - 1; toast = Toast.makeText(MainActivity.this,"字元不能超過15個",1000); toast.setGravity(Gravity.TOP, 0, 235); } public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart ,int dend){ int keep = mMaxLength - (dest.length() - (dend - dstart)); if(keep < (end - start)){ toast.show(); } if(keep <= 0){ return ""; }else if(keep >= end - start){ return null; }else{ return source.subSequence(start,start + keep); } } }}
自訂類實現InputFilter,在filter方法中做長度判斷,這樣一來,當達到最大長度之後,繼續輸入字元的話,則每次都會彈toast提示。