@TOCandroid EditText单价(Int)与数量(Float)自动相乘 保留两位小数
第一次发布文章,大家将就着看吧!!!
根据在CSDN中获取的资源加以修改已适合自己项目的开发!!!
运用正则表达式
数量(Int)范围 0-9999(最大四位数)
mPattern = Pattern.compile("(1\d{0,3}
)
∣
(
−
d
0
,
4
)|(^-\\d{0,4}
)∣(−d0,4)");
单价(Float) 范围-9999.99至9999.99
mPattern = Pattern compile("(([0]|(0[.]\d{0,2}))|([1-9]\d{0,3}(([.]\d{0,2})?)))");
先上效果图:![在这里插入图片描述]
居中的图片:
当单价和数量同时有数据时自动求积
当单价与数量只要一个数据为空时,取消自动乘积
`这是PanDuan_Float.java
`public class PanDuan_Float implements TextWatcher {
// 输入前内容
private String mBeforeText;
// 输入后内容
private String mAfterText;
// 输入框
private EditText FloatEditText, TotalEditText, IntEditText;
Activity mactivity;
/* String NumPiPei; */
// 匹配器
private Pattern mPattern;
private List<TextWatcher> mTextListWaiter = new ArrayList<TextWatcher>();
public void addTextChangedListener(TextWatcher watcher) {
mTextListWaiter.add(watcher);
}
public PanDuan_Float(EditText FloatEditText, EditText IntEditText,
EditText TotalEditText, Activity activity) {
this.FloatEditText = FloatEditText;
this.IntEditText = IntEditText;
this.TotalEditText = TotalEditText;
this.mactivity = activity;
/**
* 正则表达式匹配 条件一:如果以0开始,那么小数点前最多只有1位 条件二:小数点后面最多只有2位
* 条件三:如果不以0开始,小数点前面最多只有5位
*/
mPattern = Pattern
.compile("(([0]|(0[.]\\d{0,2}))|([1-9]\\d{0,3}(([.]\\d{0,2})?)))");
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1,
int i2) {
mBeforeText = charSequence.toString();
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (TextUtils.isEmpty(charSequence))
return;
mAfterText = charSequence.toString();
Matcher matcher = mPattern.matcher(mAfterText);
if (matcher.matches()) {
Toast.makeText(mactivity, "该数据匹配", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mactivity, "该数据不匹配", Toast.LENGTH_LONG).show();
FloatEditText.setText(mBeforeText);
// 游标移动到最后一位
FloatEditText.setSelection(FloatEditText.length());
}
for (TextWatcher textWatcher : mTextListWaiter) {
textWatcher.onTextChanged(FloatEditText.getText(), i, i1, i2);
}
}
@Override
public void afterTextChanged(Editable editable) {
String num = IntEditText.getText().toString();
String price = FloatEditText.getText().toString();
if (num.equals("")
|| num.isEmpty()||price.equals("")||price.isEmpty()) {
TotalEditText.setText("");
}
if ((!num.equals("") | !num.isEmpty())
&& (!price.equals("") | !price.isEmpty())) {
updateMoney(TotalEditText);
}
/*
* else{ updateMoney(TotalEditText); }
*/
}
private void updateMoney(EditText ed) {
try {
Object edfloat = Float.parseFloat(FloatEditText.getText()
.toString());
Object edint = Integer.parseInt(IntEditText.getText().toString());
DecimalFormat df = new DecimalFormat("###.00");
String ed12388 = df.format(edfloat);
String ed45688 = edint + "";
Toast.makeText(mactivity,
"ed12388" + ed12388 + "---ed45688" + ed45688, 1).show();
BigDecimal bdnum = new BigDecimal(ed12388);
BigDecimal bdprice = new BigDecimal(ed45688);
String tvtv1 = (bdprice.multiply(bdnum).toString()) + "";
ed.setText(tvtv1 + "");
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(mactivity, "数据错误", 1).show();
}
}
}``
这是PanDuan_Int.java
在这里插public class PanDuan_Int implements TextWatcher {
// 输入前内容
private String mBeforeText;
// 输入后内容
private String mAfterText;
// 输入框
private EditText FloatEditText, TotalEditText, IntEditText;
Activity mactivity;
// 匹配器
private Pattern mPattern;
private List<TextWatcher> mTextListWaiter = new ArrayList<TextWatcher>();
public void addTextChangedListener(TextWatcher watcher) {
mTextListWaiter.add(watcher);
}
public PanDuan_Int(EditText IntEditText, EditText FloatEditText,
EditText TotalEditText, Activity activity) {
this.IntEditText = IntEditText;
this.FloatEditText = FloatEditText;
this.TotalEditText = TotalEditText;
this.mactivity = activity;
mPattern = Pattern.compile("(^[1-9]\\d{0,3}$)|(^-\\d{0,4}$)");
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1,
int i2) {
mBeforeText = charSequence.toString();
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (TextUtils.isEmpty(charSequence))
return;
mAfterText = charSequence.toString();
Matcher matcher = mPattern.matcher(mAfterText);
if (matcher.matches()) {
/* Log.i(TAG, "匹配"); */
/* NumPiPei="Num匹配"; */
Toast.makeText(mactivity, "该数据匹配", Toast.LENGTH_LONG).show();
} else {
/* NumPiPei="Num不匹配"; */
Toast.makeText(mactivity, "该数据不匹配", Toast.LENGTH_LONG).show();
IntEditText.setText(mBeforeText);
// 游标移动到最后一位
IntEditText.setSelection(IntEditText.length());
}
for (TextWatcher textWatcher : mTextListWaiter) {
textWatcher.onTextChanged(IntEditText.getText(), i, i1, i2);
}
}
@Override
public void afterTextChanged(Editable editable) {
String num = IntEditText.getText().toString();
String price = FloatEditText.getText().toString();
if (num.equals("") || num.isEmpty() || price.equals("")
|| price.isEmpty()) {
TotalEditText.setText("");
}
if ((!num.equals("") | !num.isEmpty())
&& (!price.equals("") | !price.isEmpty())) {
updateMoney(TotalEditText);
}
}
private void updateMoney(EditText ed) {
try {
Object edfloat = Float.parseFloat(FloatEditText.getText()
.toString());
Object edint = Integer.parseInt(IntEditText.getText().toString());
DecimalFormat df = new DecimalFormat("###.00");
String ed12388 = df.format(edfloat);
String ed45688 = edint + "";
Toast.makeText(mactivity,
"ed12388" + ed12388 + "---ed45688" + ed45688, 1).show();
BigDecimal bdnum = new BigDecimal(ed12388);
BigDecimal bdprice = new BigDecimal(ed45688);
String tvtv1 = (bdprice.multiply(bdnum).toString()) + "";
ed.setText(tvtv1 + "");
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(mactivity, "数据错误", 1).show();
}
}
}
入代码片
MainActivity
在这里插入public class MainActivity extends Activity {
EditText editText1, editText2, editText3;
TextWatcher tw1, tw2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
tw1 = new PanDuan_Float(editText1, editText2, editText3,
MainActivity.this);
editText1.addTextChangedListener(tw1);
tw2 = new PanDuan_Int(editText2, editText1, editText3,
MainActivity.this);
editText2.addTextChangedListener(tw2);
}
}
代码片
DEMO链接https://download.csdn.net/download/u010232983/11458551
1-9 ↩︎