栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

android获取手机验证码界面以及倒计时实现demo,移动端应用开发试题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android获取手机验证码界面以及倒计时实现demo,移动端应用开发试题

‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’,

‘N’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’

};

private static Code bmpCode;

public static Code getInstance() {

if(bmpCode == null)

bmpCode = new Code();

return bmpCode;

}

//default settings

private static final int DEFAULT_CODE_LENGTH = 4;

private static final int DEFAULT_FONT_SIZE = 25;

private static final int DEFAULT_LINE_NUMBER = 2;

private static final int base_PADDING_LEFT = 10, RANGE_PADDING_LEFT = 15, base_PADDING_TOP = 15, RANGE_PADDING_TOP = 20;

private static final int DEFAULT_WIDTH = 100, DEFAULT_HEIGHT = 40;

//settings decided by the layout xml

//canvas width and height

private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;

//random word space and pading_top

private int base_padding_left = base_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT,

base_padding_top = base_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;

//number of chars, lines; font size

private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;

//variables

private String code;

private int padding_left, padding_top;

private Random random = new Random();

public Bitmap createBitmap() {

padding_left = 0;

Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888);

Canvas c = new Canvas(bp

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 开源分享

);

code = createCode();

c.drawColor(Color.WHITE);

Paint paint = new Paint();

paint.setTextSize(font_size);

for (int i = 0; i < code.length(); i++) {

randomTextStyle(paint);

randomPadding();

c.drawText(code.charAt(i) + “”, padding_left, padding_top, paint);

}

for (int i = 0; i < line_number; i++) {

drawLine(c, paint);

}

c.save( Canvas.ALL_SAVE_FLAG );

c.restore();//

return bp;

}

public String getCode() {

return code;

}

private String createCode() {

StringBuilder buffer = new StringBuilder();

for (int i = 0; i < codeLength; i++) {

buffer.append(CHARS[random.nextInt(CHARS.length)]);

}

return buffer.toString();

}

private void drawLine(Canvas canvas, Paint paint) {

int color = randomColor();

int startX = random.nextInt(width);

int startY = random.nextInt(height);

int stopX = random.nextInt(width);

int stopY = random.nextInt(height);

paint.setStrokeWidth(1);

paint.setColor(color);

canvas.drawLine(startX, startY, stopX, stopY, paint);

}

private int randomColor() {

return randomColor(1);

}

private int randomColor(int rate) {

int red = random.nextInt(256) / rate;

int green = random.nextInt(256) / rate;

int blue = random.nextInt(256) / rate;

return Color.rgb(red, green, blue);

}

private void randomTextStyle(Paint paint) {

int color = randomColor();

paint.setColor(color);

paint.setFakeBoldText(random.nextBoolean());

float skewX = random.nextInt(11) / 10;

skewX = random.nextBoolean() ? skewX : -skewX;

paint.setTextSkewX(skewX);

}

private void randomPadding() {

padding_left += base_padding_left + random.nextInt(range_padding_left);

padding_top = base_padding_top + random.nextInt(range_padding_top);

}

}

内部提供方法初始化和获取验证码内容

image_code.setImageBitmap(Code.getInstance().createBitmap()); //初始化验证码

Code.getInstance().getCode(); //获取验证码

计时器


public abstract class CountDownTimer {

private final long mMillisInFuture;

private final long mCountdownInterval;

// 停止时间

private long mStopTimeInFuture;

public CountDownTimer(long millisInFuture, long countDownInterval) {

mMillisInFuture = millisInFuture;

mCountdownInterval = countDownInterval;

}

public final void cancel() {

mHandler.removeMessages(MSG);

}

public synchronized final CountDownTimer start() {

if (mMillisInFuture <= 0) {

onFinish();

return this;

}

// 停止时间 = 系统启动时间 + 总计时间

mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;

mHandler.sendMessage(mHandler.obtainMessage(MSG));

return this;

}

public abstract void onTick(long millisUntilFinished);

public abstract void onFinish();

private static final int MSG = 1;

// handles counting down

private Handler mHandler = new Handler() {

@Override

public void handleMessage(Message msg) {

synchronized (CountDownTimer.this) {

// 计算剩余总时间

final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();

// 小于等于 0 ,回调 onFinish

if (millisLeft <= 0) {

onFinish();

} else if (millisLeft < mCountdownInterval) { // 小于计时间隔 ,delayed 一个消息

// no tick, just delay until done

sendMessageDelayed(obtainMessage(MSG), millisLeft);

} else {

long lastTickStart = SystemClock.elapsedRealtime();

onTick(millisLeft);

// take into account user’s onTick taking time to execute

long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();

// special case: user’s onTick took more than interval to

// complete, skip to next interval

while (delay < 0) delay += mCountdownInterval;

sendMessageDelayed(obtainMessage(MSG), delay);

}

}

}

};

使用:倒计时

final CountDownTimer timer = new CountDownTimer(60000, 1000) {

@Override

public void onTick(long millisUntilFinished) {

get_verfiy_code.setText(millisUntilFinished/1000 + “秒”);

}

@Override

public void onFinish() {

get_verfiy_code.setEnabled(true);

get_verfiy_code.setText(“获取验证码”);

}

};

点击按钮的时候:

get_verfiy_code.setEnabled(false);

timer.start();

popupwinow全部如下:

public class VerifyPopupWindow {

private EditText phone_number;

private EditText input_image_code;

private ImageView image_code;

private EditText input_code;

private Button sure;

private String inputcode;

private String realCode;

private Button no_verify;

private CustomPopWindow mPopWindow;

private Button get_verfiy_code;

private Context mContext;

public VerifyPopupWindow(Context context){

this.mContext=context;

}

public void showPopupWindow(){

View contentView = LayoutInflater.from(mContext).inflate(R.layout.user_phone_number, null);

phone_number = (EditText) contentView.findViewById(R.id.phone_number); //填入手机号

input_image_code=(EditText)contentView.findViewById(R.id.input_image_code);//填入图片验证码

image_code=(ImageView)contentView.findViewById(R.id.image_code); //图片验证码

input_code=(EditText) contentView.findViewById(R.id.input_code); //填入手机验证码

sure=(Button)contentView.findViewById(R.id.user_verfiy_submit); //确定绑定

no_verify=(Button)contentView.findViewById(R.id.ignore_verfiy_submit); //暂不验证

get_verfiy_code=(Button)contentView.findViewById(R.id.get_verfiy_code);//获取手机验证码

final CountDownTimer timer = new CountDownTimer(60000, 1000) {

@Override

public void onTick(long millisUntilFinished) {

get_verfiy_code.setText(millisUntilFinished/1000 + “秒”);

}

@Override

public void onFinish() {

get_verfiy_code.setEnabled(true);

get_verfiy_code.setText(“获取验证码”);

}

};

image_code.setImageBitmap(Code.getInstance().createBitmap()); //初始化验证码

View.onClickListener listener = new View.onClickListener() {

@Override

public void onClick(View v) {

switch (v.getId()){

case R.id.image_code:

image_code.setImageBitmap(Code.getInstance().createBitmap());

break;

case R.id.user_verfiy_submit:

if (TextUtils.isEmpty(phone_number.getText().toString())){

Toast.makeText(mContext,“请您输入手机号码”, Toast.LENGTH_SHORT).show();

return;

}

if (TextUtils.isEmpty(input_code.getText().toString())){

Toast.makeText(mContext,“请您输入手机验证码”, Toast.LENGTH_SHORT).show();

return;

}

PostFormBuilder builder= OkHttpUtils.post();

builder

.addParams(“tel”,phone_number.getText().toString())

.addParams(“telyzm”,input_code.getText().toString());

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/572898.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号