1. 程式人生 > >輸入框字數限制+動態提示:10個漢字或20個字元的問題

輸入框字數限制+動態提示:10個漢字或20個字元的問題

     EditText輸入框,要求輸入10個漢字,或者20個字元,支援中英文混輸。當輸入框內資料滿足條件時,彈出Toast進行提示。
實現:
     我是通過TextWatcher的方式,來進行實現的。即使一次貼上很多文字也不會報錯。

廢話不多言,直接上程式碼:


EditTextLimitTextWatcher:
/**
 * 對輸入框進行字數限制的textWatcher,支援中英文混輸,超出字元數會彈出Toast
 * 
 * @author Lento
 */
public class EditTextLimitTextWatcher implements TextWatcher {
    private final int mMaxLenth;

    private String mToastText;

    private Context mContext;

    private Toast mToast;

    private EditText mEditText;

    private int mCharCount;

    /**
     * @param mContext
     * @param mEditText: 需要監視的輸入框
     * @param mMaxLenth :支援輸入的最大字元數(1個漢字為2個字元,1個英文字母為1個字元)
     * @param mToastText: 輸入字元數超出最大值時的toast文字提示,為null時,不提示
     */
    public EditTextLimitTextWatcher(Context mContext, EditText mEditText, int mMaxLenth,
            String mToastText) {
        this.mContext = mContext;
        this.mMaxLenth = mMaxLenth;
        this.mToastText = mToastText;
        this.mEditText = mEditText;
    }

    /**
     * 避免多次重複彈出toast
     * 
     * @param text
     */
    private void showToast(String text) {
        if (mToast == null) {
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(text);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        mCharCount = before + count;
        if (mCharCount > mMaxLenth) {
            mEditText.setSelection(mEditText.length());
        }
        try {
            mCharCount = mEditText.getText().toString().getBytes("GBK").length;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (mCharCount > mMaxLenth) {
            CharSequence subSequence = null;
            for (int i = 0; i < s.length(); i++) {
                subSequence = s.subSequence(0, i);
                try {
                    if (subSequence.toString().getBytes("GBK").length == mCharCount) {
                        mEditText.setText(subSequence.toString());
                        break;
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (!TextUtils.isEmpty(mToastText)) {
                showToast(mToastText);
            }
            String androidVersion = android.os.Build.VERSION.RELEASE;
            if (androidVersion.charAt(0) >= '4') {
                mEditText.setText(subSequence.toString());
            }
        }
    }
}

 使用:MainActivity
 
public class MainActivity extends Activity {

    private EditText mEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = (EditText)findViewById(R.id.et);
        EditTextLimitTextWatcher mTextWatcher = new EditTextLimitTextWatcher(this, 20, "字數超出限制了!");
        mEditText.addTextChangedListener(mTextWatcher);
    }

XML佈局檔案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_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"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

效果,上圖:
當超出10個漢字,或者20個字元時:




參考文章:
http://blog.csdn.net/scyatcs/article/details/8798810
http://blog.csdn.net/liujianminghero/article/details/7092236 

PS:
已知的小Bug:當輸入一段文字後,把游標移至文字中間,輸入文字後,游標都會移至最末。有哪位童鞋可以解決的話,懇請指出,拜謝。