限制EditText输入字符及其事件监听

package org.sina.android;


import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

 //限制EditText输入字符

public class AndroidTestActivity extends Activity {

    private EditText inputa;
    private EditText inputb;
    private TextView result;

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

        inputa = (EditText) findViewById(R.id.inputa);
        inputb = (EditText) findViewById(R.id.inputb);

        result = (TextView) findViewById(R.id.result);

        inputa.addTextChangedListener(new TextFilter(inputa));
        inputb.addTextChangedListener(new TextFilter(inputb));
    }

    private boolean textFilter(String text) {

        boolean isLegal = false;
//trim()为String类方法,去掉字符串两端空格
        if (null != text && !"".equals(text.trim())) {
            if (text.indexOf(".") > 0) {
                if (text.endsWith(".")) {
                    isLegal = text.matches("^\\d{1,4}\\.$");
                } else {
                    isLegal = text.matches("^\\d{1,4}\\.\\d$");
                }
            } else {
                isLegal = text.matches("^\\d{1,4}$");
            }
        }
        return isLegal;
    }

    class TextFilter implements TextWatcher {

        private EditText editText;

        private String rt;

        public TextFilter(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            Log.w("beforeTextChanged", "-----------------------------------");
            Log.e("beforeTextChanged@ ", " CharSequence: " + s + " -start: "
                    + start + " -count: " + count);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            Log.w("onTextChanged", "-----------------------------------");
            Log.e("onTextChanged@ ", "CharSequence: " + s + " -start: " + start
                    + " before: " + before + " -count: " + count);

            String a = inputa.getText().toString();
            String b = inputb.getText().toString();
            if (null != s && !"".equals(s.toString().trim())) {
                String input = s.toString();
                if (textFilter(input)) {
                    if (isEmpty(a) || isEmpty(b)) {
                        result.setText("0.00");
                    } else {
                        double ad = Double.parseDouble(a);
                        double bd = Double.parseDouble(b);
                        result.setText(String.format("%1$.2f", (ad * bd)));
                    }
                } else {
                    editText.setText(input.substring(0, start)
                            + input.substring(start + count));
                    editText.requestFocus();
                    editText.setSelection(start);
                }
            }
            else
            {
                checkResult(a, b);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            // editText.setText(rt);
            Log.w("afterTextChanged", "-----------------------------------");
            Log.e("afterTextChanged@ ", s.toString());
        }
        private void checkResult(String a, String b)
        {
            if (!Utils.isEmpty(a) && !Utils.isEmpty(b))
            {
                return;
            }
            String resultStr = trim(a) + trim(b);
            if (isEmpty(resultStr))
            {
                result.setText("");
            }
            else
            {
                result.setText("0.00");
            }
        }

    }

    private boolean isEmpty(String str) {
        return null == str || "".equals(str.trim());

    }
    /**
     * 去空格
     * 
     * @param o trim对象
     * @return trim后的对象
     * @see [类、类#方法、类#成员]
     */
    public static String trim(Object o)
    {
        String str = "";
        if (null != o)
        {
            str = o.toString().trim();
        }
        return str;
    }
} 

本文原始代码摘自http://ice-key.iteye.com/blog/1813914

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值