package com.example.mathedittextdemo;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
/**
* @author wilin
*/
public class MainActivity extends Activity implements TextWatcher {
private EditText editText;
private int index = 0; // 输入内容前的下标
private boolean isSettext = false;
private int time = 0; // time 标识输入字符数,用来判断长度是否超过0.00的长度,即4 = 3+1
private String originString; // 输入前的字符串,判断是输入还是删除
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edittext);
editText.setText("0.00");
editText.setSelection(4);
editText.addTextChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
index = editText.getSelectionEnd();
originString = new String(s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!isSettext) {// 为使在setText的时候不重复调用
if (s.length() > originString.length()) { // 输入数字
String newString = new String(s.toString());
newString = newString.replace(".", "");
if (time < 3 && index == newString.length() && newString.startsWith("0")) {
newString = newString.substring(1, newString.length());
}
String a = String.valueOf(Long.valueOf(newString.substring(0, newString.length() - 2))); // 整数部分
StringBuilder sb = new StringBuilder(a);
sb.append(".").append(newString.subSequence(newString.length() - 2,newString.length())); // 小数部分
String tmp = sb.toString();
isSettext = true;
editText.setText(tmp); // 更新数字
editText.setSelection(tmp.length());
time++;
} else { // 删除数字
if (time != 0) {
String newString = new String(s.toString());
newString = newString.replace(".", "");
if (newString.length() < 3) //如果没有整数部分,整数部分置0
newString = "0".concat(newString);
StringBuilder sb = new StringBuilder(newString.substring(0, newString.length() - 2)).append(".")
.append(newString.subSequence(newString.length() - 2,newString.length()));
String tmp = sb.toString();
isSettext = true;
editText.setText(tmp);
editText.setSelection(tmp.length());
time--;
} else { // 删除掉所有输入
isSettext = true;
editText.setText("0.00");
// index = 4;
editText.setSelection(4);
}
}
} else {
isSettext = false;
editText.setSelection(s.length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
}
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
/**
* @author wilin
*/
public class MainActivity extends Activity implements TextWatcher {
private EditText editText;
private int index = 0; // 输入内容前的下标
private boolean isSettext = false;
private int time = 0; // time 标识输入字符数,用来判断长度是否超过0.00的长度,即4 = 3+1
private String originString; // 输入前的字符串,判断是输入还是删除
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edittext);
editText.setText("0.00");
editText.setSelection(4);
editText.addTextChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
index = editText.getSelectionEnd();
originString = new String(s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!isSettext) {// 为使在setText的时候不重复调用
if (s.length() > originString.length()) { // 输入数字
String newString = new String(s.toString());
newString = newString.replace(".", "");
if (time < 3 && index == newString.length() && newString.startsWith("0")) {
newString = newString.substring(1, newString.length());
}
String a = String.valueOf(Long.valueOf(newString.substring(0, newString.length() - 2))); // 整数部分
StringBuilder sb = new StringBuilder(a);
sb.append(".").append(newString.subSequence(newString.length() - 2,newString.length())); // 小数部分
String tmp = sb.toString();
isSettext = true;
editText.setText(tmp); // 更新数字
editText.setSelection(tmp.length());
time++;
} else { // 删除数字
if (time != 0) {
String newString = new String(s.toString());
newString = newString.replace(".", "");
if (newString.length() < 3) //如果没有整数部分,整数部分置0
newString = "0".concat(newString);
StringBuilder sb = new StringBuilder(newString.substring(0, newString.length() - 2)).append(".")
.append(newString.subSequence(newString.length() - 2,newString.length()));
String tmp = sb.toString();
isSettext = true;
editText.setText(tmp);
editText.setSelection(tmp.length());
time--;
} else { // 删除掉所有输入
isSettext = true;
editText.setText("0.00");
// index = 4;
editText.setSelection(4);
}
}
} else {
isSettext = false;
editText.setSelection(s.length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
}