Android开发-Android中金额,身份z,邮箱,日期等常用判断

Android开发-Android中金额,身份z,邮箱,日期等常用判断,第1张

概述简介:对Android开发中,常用的输入金额判断,输入金额是否能进行提现;输入的身份z简单判断;输入是否是日期格式,输入的手机号是否符合规范等。 步骤一:布局文件activity_main.xml<LinearLayoutxmlns:android="http://schemas.android.com/apkes/android"   xmlns:tools="htt

简介:

对AndroID开发中,常用的输入金额判断,输入金额是否能进行提现;输入的身份z简单判断;输入是否是日期格式,输入的手机号是否符合规范等。

 

步骤一:布局文件

activity_main.xml

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"

    xmlns:tools="http://schemas.androID.com/tools"

    androID:layout_wIDth="match_parent"

    androID:layout_height="match_parent"

    androID:orIEntation="vertical"  >

 

    <EditText

        androID:ID="@+ID/edittext1"

        androID:layout_height="wrap_content"

        androID:layout_wIDth="match_parent"

        androID:digits="0123456789."

        androID:inputType="number"

        androID:hint="只能输入金额"

        />

    <button

        androID:ID="@+ID/button1"

        androID:layout_wIDth="wrap_content"

        androID:layout_height="wrap_content"

        androID:textSize="15dp"

        androID:text="提现按钮\n(输入金额要大于100可以提现)"/>

 

    <EditText

        androID:ID="@+ID/edittext2"

        androID:layout_height="wrap_content"

        androID:layout_wIDth="match_parent"

        androID:hint="只能输入电话号码"

        />

    <EditText

        androID:ID="@+ID/edittext3"

        androID:layout_height="wrap_content"

        androID:layout_wIDth="match_parent"

        androID:maxLength="11"

        androID:inputType="number"

        androID:hint="只能输入手机号"

        />

 

    <EditText

        androID:ID="@+ID/edittext4"

        androID:layout_height="wrap_content"

        androID:layout_wIDth="match_parent"

        androID:hint="只能输入邮箱"

        />

 

    <EditText

        androID:ID="@+ID/edittext5"

        androID:layout_height="wrap_content"

        androID:layout_wIDth="match_parent"

        androID:hint="只能输入日期格式:yyyy-mm-dd"

        />

 

    <EditText

        androID:ID="@+ID/Card"

        androID:layout_wIDth="match_parent"

        androID:layout_height="wrap_content"

        androID:digits="0123456789xX"

        androID:hint="请输入您的身份z号"

        androID:inputType="number"

        androID:maxLength="18"

        androID:numeric="integer"

        androID:singleline="true"  />

 

</linearLayout>

 

步骤二:主界面 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) {

                // 判断邮箱

                //对于www.1257683526@foxmail.com加前面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中身份z判断是否正确

Demo样例下载:

http://www.huwan.xin/Source/AppPackage/ZIP/Android/CheckInputDemo.rar

更多内容请访问:www.huwan.xin

 

总结

以上是内存溢出为你收集整理的Android开发-Android中金额,身份z,邮箱,日期等常用判断全部内容,希望文章能够帮你解决Android开发-Android中金额,身份z,邮箱,日期等常用判断所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1062260.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存