安卓 生成随机验证码

本文介绍了如何在Android应用中动态生成随机验证码。通过展示XML布局和MainActivity的代码,演示了如何创建输入框和验证码图片,并监听点击事件来刷新验证码。用户输入的验证码与系统生成的验证码进行比较,以验证其准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 动态生成随机验证码:

效果图:
  

XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:background="@drawable/security_code_bg" >

            <EditText
                android:id="@+id/et_identify_code"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@null"
                android:hint="请输入右侧验证码" />
        </LinearLayout>

        <ImageView
            android:id="@+id/iv_showCode"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp" />
    </LinearLayout>

    <Button
        android:id="@+id/but_forgetpass_toSetCodes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@drawable/buttonshape"
        android:text="提交验证码"
        android:textColor="@color/main_color_white" />

</LinearLayout>

MainActivity:
package com.example.securitycodedemo;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity implements OnClickListener{

    private ImageView iv_showCode;
    private EditText et_identify_code;
    //产生的验证码
    private String realCode;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_identify_code = (EditText) findViewById(R.id.et_identify_code);
        Button but_toSetCode = (Button) findViewById(R.id.but_forgetpass_toSetCodes);
        but_toSetCode.setOnClickListener(this);
        iv_showCode = (ImageView) findViewById(R.id.iv_showCode);
        //将验证码用图片的形式显示出来
        iv_showCode.setImageBitmap(RandomCode.getInstance().createBitmap());
        realCode = RandomCode.getInstance().getCode().toLowerCase();
        iv_showCode.setOnClickListener(this);
        
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
        case R.id.iv_showCode:
            iv_showCode.setImageBitmap(RandomCode.getInstance().createBitmap());
            realCode = RandomCode.getInstance().getCode().toLowerCase();
            break;
        case R.id.but_forgetpass_toSetCodes:
            String identify_code = et_identify_code.getText().toString().toLowerCase();

            if (identify_code.equals(realCode)) {
                Toast.makeText(MainActivity.this, "验证码正确", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
            }
            break;
		}
	}

}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值