PriceTextWatcher.java
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.xdy.commonlibrary.adapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import java.lang.ref.WeakReference;
/**
* Created by Administrator on 2017/5/20.
*/
public class PriceTextWatcher<T extends PriceTextWatcher.UpdateEntityPrice> implements TextWatcher {
private WeakReference<EditText> mEditText;
private T t;
private int numMax = 8;//默认可以输入8位小数
public PriceTextWatcher(EditText editText) {
mEditText = new WeakReference<>(editText);
}
public void setNumMax(int numMax) {
if (numMax > 0)
this.numMax = numMax;
}
public void setSyncEntity(T t) {
this.t = t;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//如果有小数点的情况下
EditText editText = null;
if ((editText = mEditText.get()) != null) {
//如果有小数点的情况下
if (s.toString().contains(".")) {
if (s.length() - 1 - s.toString().indexOf(".") > 2) {
s = s.toString().subSequence(0,
s.toString().indexOf(".") + 3);
editText.setText(s);
editText.setSelection(s.length());
}
} else {
if (s.toString().length() > numMax) {
s = s.toString().subSequence(0, numMax);
editText.setText(s);
editText.setSelection(s.length());
}
}
//当是“.”开头的时候 直接输入为0.XX
if (s.toString().trim().substring(0).equals(".")) {
s = "0" + s;
editText.setText(s);
editText.setSelection(2);
}
// 如果是0开头的话 只能输入 0.XX
if (s.toString().startsWith("0")
&& s.toString().trim().length() > 1) {
if (!s.toString().substring(1, 2).equals(".")) {
editText.setText(s.subSequence(0, 1));
editText.setSelection(1);
return;
}
}
}
}
@Override
public void afterTextChanged(Editable s) {
// String temp = s.toString();
// if (!TextUtils.isEmpty(temp)) {
// Double price = Double.valueOf(s.toString());
// if (price > 0) {
// if (t != null)
// t.setPrice(price);
// }
// }
}
public interface UpdateEntityPrice {
public void setPrice(double price);
}
}