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;
}
}
限制EditText输入字符及其事件监听
最新推荐文章于 2023-09-21 09:55:39 发布