<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
android:layout_marginLeft="@dimen/dp_12"
android:layout_marginRight="@dimen/dp_12"
android:layout_marginTop="@dimen/dp_12"
android:background="@drawable/bg_white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="@dimen/dp_10"
android:text="消费金额:"
android:textColor="@color/title_color"
android:textSize="@dimen/sp_14" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_buf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/dp_10"
android:gravity="center|right"
android:text="¥"
android:textColor="@color/shop_guige"
android:textSize="@dimen/sp_20"
android:visibility="gone" />
<EditText
android:id="@+id/dealMoney_et"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@null"
android:gravity="right|center"
android:inputType="number|numberDecimal"
android:paddingLeft="@dimen/dp_5"
android:paddingRight="@dimen/dp_10"
android:textColor="@color/shop_guige"
//@null表示和文字颜色一样
android:textCursorDrawable="@null"
android:textSize="@dimen/sp_14" />
<TextView
android:id="@+id/tv_hint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="8dp"
android:background="@null"
android:gravity="right|center_vertical"
android:text="@string/询问服务员后输入"
android:textColor="@color/third_txt_color"
android:textSize="@dimen/sp_14" />
</FrameLayout>
</LinearLayout>
screenshot-1510135622138.jpg
1.输入过滤器InputFilter约束用户输入
public class CashierInputFilter implements InputFilter {
Pattern mPattern;
//输入的最大金额
private static final int MAX_VALUE = 100000;
//小数点后的位数
private static final int POINTER_LENGTH = 2;
private static final String POINTER = ".";
private static final String ZERO = "0";
//只能输入数字和小数点
public CashierInputFilter() {
mPattern = Pattern.compile("([0-9]|\\.)*");
}
/**
* @param source 新输入的字符串
* @param start 新输入的字符串起始下标,一般为0
* @param end 新输入的字符串终点下标,一般为source长度-1
* @param dest 输入之前文本框内容
* @param dstart 原内容起始坐标,一般为0
* @param dend 原内容终点坐标,一般为dest长度-1
* @return 输入内容
*/
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String sourceText = source.toString();
String destText = dest.toString();
//验证删除等按键
if (TextUtils.isEmpty(sourceText)) {
return "";
}
Matcher matcher = mPattern.matcher(source);
//已经输入小数点的情况下,只能输入数字
if (destText.contains(POINTER)) {
if (!matcher.matches()) {
return "";
} else {
if (POINTER.equals(source.toString())) { //只能输入一个小数点
return "";
}
}
//验证小数点精度,保证小数点后只能输入两位
int index = destText.indexOf(POINTER);
int length = dend - index;
if (length > POINTER_LENGTH) {
return dest.subSequence(dstart, dend);
}
} else {
/**
* 没有输入小数点的情况下,只能输入小数点和数字
* 1. 首位不能输入小数点
* 2. 如果首位输入0,则接下来只能输入小数点了
*/
if (!matcher.matches()) {
return "";
} else {
if ((POINTER.equals(source.toString())) && TextUtils.isEmpty(destText)) { //首位不能输入小数点
return "";
} else if (!POINTER.equals(source.toString()) && ZERO.equals(destText)) { //如果首位输入0,接下来只能输入小数点
return "";
}
}
}
//验证输入金额的大小
double sumText = Double.parseDouble(destText + sourceText);
if (sumText > MAX_VALUE) {
return dest.subSequence(dstart, dend);
}
return dest.subSequence(dstart, dend) + sourceText;
}
}
在Activity中的代码
InputFilter[] filters = {new CashierInputFilter()};
etDealMoney.setFilters(filters);
NoConcessionsMoneyEd.setFilters(filters);
/**
* 监听消费金额的输入
*/
dealMoneyTv.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
conPrice = s.toString();
int bg = TextUtils.isEmpty(s) ? R.drawable.selector_tuan__offer_to_pay_butn_bg : R.drawable.tuan_red_bag;
SubmitButtonTv.setBackground(getResources().getDrawable(bg));
SubmitButtonTv.setEnabled(TextUtils.isEmpty(s) ? false : true);
dealWithMone(conPrice, notEnjoy);
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
//设置不享受优惠编辑框可以编辑
NoConcessionsMoneyEd.setMoneyIsEmpty(false);
//显示消费金额的¥
tvBuf.setVisibility(View.VISIBLE);
//获取EditText内容长度
float textWidth = getTextWidth(dealMoneyTv);
//通过setX(float x)让¥往左移动
tvBuf.setX(-textWidth);
//隐藏hint
tvHint.setVisibility(View.GONE);
} else {
NoConcessionsMoneyEd.setMoneyIsEmpty(true);
tvBuf.setVisibility(View.GONE);
tvHint.setVisibility(View.VISIBLE);
}
}
/**
* 监听不享受优惠金额的输入
*/
NoConcessionsMoneyEd.setOnFocusChangeListener((v, hasFocus) -> {
//不享受优惠金额的焦点
noconcessionsFocus = hasFocus;
});
NoConcessionsMoneyEd.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//不享受优惠金额
notEnjoy = s.toString();
if (!TextUtils.isEmpty(conPrice)) {
dealWithMone(conPrice, notEnjoy);
}
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
//不享受优惠金额¥显示
tvSymbol.setVisibility(View.VISIBLE);
//获取内容长度
float textWidth = getTextWidth(NoConcessionsMoneyEd);
tvSymbol.setX(-textWidth);
} else {
tvSymbol.setVisibility(View.GONE);
}
}
});
不享受优惠金额这里使用了自定义EditText
public class MyEditText extends EditText {
/**
* 消费金额是否为空
* 默认为true
*/
private boolean isEmpty = true;
public MyEditText(Context context) {
super(context);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
return new InputConnecttion(super.onCreateInputConnection(outAttrs), false);
}
/**
* 消费金额是否为空
*
* @param isEmpty
*/
public void setMoneyIsEmpty(boolean isEmpty) {
this.isEmpty = isEmpty;
}
class InputConnecttion extends InputConnectionWrapper implements
InputConnection {
public InputConnecttion(InputConnection target, boolean mutable) {
super(target, mutable);
}
/**
* 对输入的内容进行拦截
*
* @param text
* @param newCursorPosition
* @return
*/
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
// 当消费金额为空的时候进行拦截
if (isEmpty) {
ToastUtil.show("请先填写消费金额");
return false;
}
return super.commitText(text, newCursorPosition);
}
@Override
public boolean sendKeyEvent(KeyEvent event) {
return super.sendKeyEvent(event);
}
@Override
public boolean setSelection(int start, int end) {
return super.setSelection(start, end);
}
}
}
/**
* 获取EditText的内容宽度
* @param editText
* @return
*/
private float getTextWidth(EditText editText) {
TextPaint mTextPaint = editText.getPaint();
return mTextPaint.measureText(editText.getText().toString());
}