安卓 生成随机验证码

    xiaoxiao2021-11-22  59

    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; } } }
    转载请注明原文地址: https://ju.6miu.com/read-678442.html

    最新回复(0)