简介:
对Android开发中,常用的输入金额判断,输入金额是否能进行提现;输入的身份证简单判断;输入是否是日期格式,输入的手机号是否符合规范等。
步骤一:布局文件
activity_main.xml
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > android:id="@+id/edittext1" android:layout_height="wrap_content" android:layout_width="match_parent" android:digits="0123456789." android:inputType="number" android:hint="只能输入金额" />
步骤二:主界面 MainActivity.java
package com.huwan.checkinputdemo;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 提现按钮
// 对按钮增加了可以是否可以点击的判断,如果输入金额错误,直接不让用户点击,也可以将金额判断是否正确做成全局参数,在提现按钮中进行判断
// 即:增加个全局的boolean money = false;然后在button1.setClickable(false)的位置上下,将金额设置属性,然后在点击的时候判断属性
final Button button1 = findViewById(R.id.button1);
//1.验证输入的是金额
final EditText edittext1 = (EditText) findViewById(R.id.edittext1);
edittext1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 输入的是金额,不是字体变红
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
java.util.regex.Matcher match = pattern.matcher(edittext1.getText().toString());
if(match.matches() == false){
edittext1.setTextColor(Color.parseColor("#ff0000"));
Toast.makeText(getBaseContext(), "您输入的金额有误", Toast.LENGTH_SHORT).show();
button1.setClickable(false);
}
else {
edittext1.setTextColor(Color.parseColor("#000000"));
button1.setClickable(true);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String moneyMount = edittext1.getText().toString().trim();
Double miniMoney1 = Double.parseDouble("100.00"); // 这个100.00是最低提现金额
Double availableMoney1 = Double.parseDouble("500.00"); // 这个500.00是服务器获取的当前可提现的金额
if(!"".equals(moneyMount) //并非没有输入
&& Double.parseDouble(moneyMount) > miniMoney1 //大于最低提现金额
&& Double.parseDouble(moneyMount) <= availableMoney1 //比较他两个大小
&& !"0.00".equals(moneyMount) //输入的不是零
&& !moneyMount.toString().equals("0") //输入的不是零
&& !moneyMount.toString().startsWith("0") //不是以0开头
&& moneyMount.toString().trim().length() != 0){ //没有输入
Toast.makeText(getBaseContext(), "执行提现操作", Toast.LENGTH_SHORT).show();
}else if(moneyMount.equals("")){
Toast.makeText(getBaseContext(), "您还没有输入转出金额", Toast.LENGTH_SHORT).show();
}else if(Double.parseDouble(moneyMount) > availableMoney1){
Toast.makeText(getBaseContext(), "您输入转出金额大于余额", Toast.LENGTH_SHORT).show();
}else if(Double.parseDouble(moneyMount) < miniMoney1){
Toast.makeText(getBaseContext(), "最低转出金额为100.00元", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "您输入的金额错误", Toast.LENGTH_SHORT).show();
}
}
});
//2.验证输入的是电话号码
final EditText edittext2 = (EditText) findViewById(R.id.edittext2);
edittext2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 输入的是电话号码,不是字体变红
//事例:0551-5565654才会显示是黑色的
String phone = "0\\d{2,3}-\\d{7,8}";
Pattern p = Pattern.compile(phone);
Matcher m = p.matcher(edittext2.getText().toString());
if(m.matches()){
edittext2.setTextColor(Color.parseColor("#000000"));
}
else {
edittext2.setTextColor(Color.parseColor("#ff0000"));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
//3.验证输入的是手机号码
final EditText edittext3 = (EditText) findViewById(R.id.edittext3);
edittext3.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 判断手机号
Pattern p = Pattern.compile("^[1][0-9][0-9]{9}$");
Matcher m = p.matcher(edittext3.getText().toString());
if(m.matches()){
edittext3.setTextColor(Color.parseColor("#000000"));
}
else {
edittext3.setTextColor(Color.parseColor("#ff0000"));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
//4.验证输入的是邮箱
final EditText edittext4 = (EditText) findViewById(R.id.edittext4);
edittext4.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 判断邮箱
//对于[email protected]加前面www识别不了
String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
Pattern p = Pattern.compile(str);
Matcher m = p.matcher(edittext4.getText().toString());
if(m.matches()){
edittext4.setTextColor(Color.parseColor("#000000"));
}
else {
edittext4.setTextColor(Color.parseColor("#ff0000"));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
//5.验证输入的是时间格式
final EditText edittext5 = (EditText) findViewById(R.id.edittext5);
edittext5.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// 5.验证输入的是时间格式
//举例:2016.08.02才是正确的
String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
+ "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
+ "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
+ "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
+ "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
if ((edittext5.getText().toString() != null)) {
Pattern pattern = Pattern.compile(datePattern1);
Matcher match = pattern.matcher(edittext5.getText().toString());
if (match.matches()) {
pattern = Pattern.compile(datePattern2);
match = pattern.matcher(edittext5.getText().toString());
edittext5.setTextColor(Color.parseColor("#000000"));
} else {
edittext5.setTextColor(Color.parseColor("#ff0000"));
}
}else{
edittext5.setTextColor(Color.parseColor("#ff0000"));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
参考阅读:Android开发-Android中身份证判断是否正确
Demo样例下载:
http://www.huwan.xin/Source/AppPackage/ZIP/Android/CheckInputDemo.rar
更多内容请访问:www.huwan.xin