1、只能输入小数点后两位(0.00)
//输入框监听
binding.enterAmount.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) {
}
@Override
public void afterTextChanged(Editable s) {
if (canDrawMoney >= limitWithDraw) {
String temp = s.toString();
//返回“.”的位置
int posDot = temp.indexOf(".");
if (posDot <= 0) {
// return;
} else if (temp.length() - posDot - 1 > 2) {
s.delete(posDot + 3, posDot + 4);
}
if (StringUtil.getString2Double(s.toString()) * 100 > canDrawMoney) {
binding.withdarwMoney.setTextColor(getResources().getColor(R.color.tip_red));
binding.withdarwMoney.setText("金额已超过可提现余额");
binding.withdarw.setEnabled(false);
binding.withdarw.setBackgroundResource(R.mipmap.ic_bg_bt_gray);
} else {
binding.withdarwMoney.setTextColor(getResources().getColor(R.color.tip_gray));
binding.withdarwMoney.setText("可提现余额 " + CommonUtils.toPrice(canDrawMoney) + "元");
binding.withdarw.setEnabled(true);
binding.withdarw.setBackgroundResource(R.mipmap.ic_bg_bt_blue);
}
} else {
binding.withdarwMoney.setTextColor(getResources().getColor(R.color.tip_gray));
binding.withdarwMoney.setText("可提现余额 " + CommonUtils.toPrice(canDrawMoney) + "元");
binding.withdarw.setEnabled(false);
binding.withdarw.setBackgroundResource(R.mipmap.ic_bg_bt_gray);
}
}
});
二、只能输入正整数并且0开头无法输入数字
String text = s.toString();
int len = s.toString().length();
if (len > 1 && text.startsWith("0")) {
s.replace(1, 2, "");//输入0后无法输入任何数字
// s.clear();
}
xml文件
<EditText
android:id="@+id/enter_amount"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginLeft="@dimen/activity_length_dp_3"
android:layout_weight="8"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入金额"
android:inputType="number"
android:maxLength="10"
android:maxLines="1"
android:textColor="@color/black"
android:textSize="@dimen/activity_word_sp_18" />