昨天写了个小Demo,实现了随机生成验证码,和锁定EditText两个小功能,先看一下效果图:
锁定EditText在我们不须要用户编辑EditText内容的时候能够用到,实现还是非常easy的,一行代码:
etLock.setEnabled(false);
随机生成验证码,主要是用了Random函数,以及将View转为Bitmap的逻辑,也没有难点,以下贴一下代码,供须要的朋友參考:
Main.java
package com.zms.textlock; import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView; import java.util.Random; public class Main extends ActionBarActivity {
private Button btnLock;
private EditText etLock;
private String numStrTmp = "";
private String numStr = "";
private int[] numArray = new int[4];
private int[] colorArray = new int[6]; private TextView tvHideA;
private TextView tvHideB;
private TextView tvHideC;
private TextView tvHideD;
private ImageView ivNumA;
private ImageView ivNumB;
private ImageView ivNumC;
private ImageView ivNumD;
private Button btnCheck;
private TextView tvCheck;
private EditText etCheck; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLock = (Button) findViewById(R.id.btnLock);
etLock = (EditText) findViewById(R.id.etLock);
btnLock.setOnClickListener(new onClickListenerImp()); tvHideA = (TextView) findViewById(R.id.tvHideA);
tvHideB = (TextView) findViewById(R.id.tvHideB);
tvHideC = (TextView) findViewById(R.id.tvHideC);
tvHideD = (TextView) findViewById(R.id.tvHideD);
ivNumA = (ImageView) findViewById(R.id.ivNumA);
ivNumB = (ImageView) findViewById(R.id.ivNumB);
ivNumC = (ImageView) findViewById(R.id.ivNumC);
ivNumD = (ImageView) findViewById(R.id.ivNumD); tvCheck = (TextView) findViewById(R.id.tvCheck);
etCheck = (EditText) findViewById(R.id.etCheck);
btnCheck = (Button) findViewById(R.id.btnCheck);
btnCheck.setOnClickListener(new onClickListenerImp()); setNum();
} private class onClickListenerImp implements View.OnClickListener {
@Override
public void onClick(View view) {
if (view == btnLock) {
if (etLock.isEnabled()) {
etLock.setEnabled(false);
btnLock.setText("解锁");
} else {
etLock.setEnabled(true);
btnLock.setText("锁定");
}
} else if (view == btnCheck) {
if (etCheck.getText().toString() != null && etCheck.getText().toString().trim().length() > 0) {
tvCheck.setVisibility(View.VISIBLE);
if (numStr.equals(etCheck.getText().toString())) {
tvCheck.setText("输入正确");
tvCheck.setTextColor(Color.GREEN);
} else {
tvCheck.setText("输入错误");
tvCheck.setTextColor(Color.RED);
}
} else {
setNum();
tvCheck.setVisibility(View.GONE);
//Toast.makeText(Main.this, "Please Input Verify Code", Toast.LENGTH_SHORT).show();
}
}
}
} public void initNum() {
numStr = "";
numStrTmp = "";
for (int i = 0; i < numArray.length; i++) {
int numIntTmp = new Random().nextInt(10);
numStrTmp = String.valueOf(numIntTmp);
numStr = numStr + numStrTmp;
numArray[i] = numIntTmp;
}
} public void setNum() { initNum();
tvHideA.setText("" + numArray[0]);
tvHideA.setTextColor(randomColor());
tvHideB.setText("" + numArray[1]);
tvHideB.setTextColor(randomColor());
tvHideC.setText("" + numArray[2]);
tvHideC.setTextColor(randomColor());
tvHideD.setText("" + numArray[3]);
tvHideD.setTextColor(randomColor()); // Num 1
Matrix matrixA = new Matrix();
matrixA.reset();
matrixA.setRotate(randomAngle());
Bitmap bmNumA = Bitmap.createBitmap(getBitmapFromView(tvHideA, 20, 50), 0, 0, 20, 50, matrixA, true);
ivNumA.setImageBitmap(bmNumA);
// Num 2
Matrix matrixB = new Matrix();
matrixB.reset();
matrixB.setRotate(randomAngle());
Bitmap bmNumB = Bitmap.createBitmap(getBitmapFromView(tvHideB, 20, 50), 0, 0, 20, 50, matrixB, true);
ivNumB.setImageBitmap(bmNumB);
// Num 3
Matrix matrixC = new Matrix();
matrixC.reset();
matrixC.setRotate(randomAngle());
Bitmap bmNumC = Bitmap.createBitmap(getBitmapFromView(tvHideC, 20, 50), 0, 0, 20, 50, matrixC, true);
ivNumC.setImageBitmap(bmNumC);
// Num 4
Matrix matrixD = new Matrix();
matrixD.reset();
matrixD.setRotate(randomAngle());
Bitmap bmNumD = Bitmap.createBitmap(getBitmapFromView(tvHideD, 20, 50), 0, 0, 20, 50, matrixD, true);
ivNumD.setImageBitmap(bmNumD);
} public int randomAngle() {
return 20 * (new Random().nextInt(5) - new Random().nextInt(3));
} public int randomColor() {
colorArray[0] = 0xFF000000; // BLACK
colorArray[1] = 0xFFFF00FF; // MAGENTA
colorArray[2] = 0xFFFF0000; // RED
colorArray[3] = 0xFF00FF00; // GREEN
colorArray[4] = 0xFF0000FF; // BLUE
colorArray[5] = 0xFF00FFFF; // CYAN
//colorArray[6] = 0xFFFFFF00; // YELLOW:看不清楚 int randomColorId = new Random().nextInt(6);
return colorArray[randomColorId];
} public static Bitmap getBitmapFromView(View view, int width, int height) {
int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
view.measure(widthSpec, heightSpec);
view.layout(0, 0, width, height);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas); return bitmap;
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
布局文件: 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".Main"> <TextView
android:layout_width="wrap_content"
android:text="锁定EditText内容:"
android:layout_height="wrap_content" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <EditText
android:id="@+id/etLock"
android:layout_width="200dp"
android:layout_height="wrap_content" /> <Button
android:id="@+id/btnLock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="锁定" />
</LinearLayout> <TextView
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="随机生成4位验证码:" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <LinearLayout
android:id="@+id/layVerify"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <TextView
android:id="@+id/tvHideA"
android:layout_width="70dp"
android:layout_height="70dp"
android:textSize="30dp"
android:gravity="center"
android:visibility="gone" /> <TextView
android:id="@+id/tvHideB"
android:layout_width="70dp"
android:layout_height="70dp"
android:textSize="30dp"
android:gravity="center"
android:visibility="gone" /> <TextView
android:id="@+id/tvHideC"
android:layout_width="70dp"
android:layout_height="70dp"
android:textSize="30dp"
android:gravity="center"
android:visibility="gone" /> <TextView
android:id="@+id/tvHideD"
android:layout_width="70dp"
android:layout_height="70dp"
android:textSize="30dp"
android:gravity="center"
android:visibility="gone" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content"> <ImageView
android:id="@+id/ivNumA"
android:layout_width="50dp"
android:layout_height="70dp" /> <ImageView
android:id="@+id/ivNumB"
android:layout_width="50dp"
android:layout_height="70dp" /> <ImageView
android:id="@+id/ivNumC"
android:layout_width="50dp"
android:layout_height="70dp" /> <ImageView
android:id="@+id/ivNumD"
android:layout_width="50dp"
android:layout_height="70dp" />
</LinearLayout> </LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <EditText
android:id="@+id/etCheck"
android:hint="验证码"
android:layout_width="80dp"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/tvCheck"
android:text="结果"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> <Button
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
</LinearLayout>
有疑问欢迎交流~
还是上传一下代码吧,是用Android Studio写的。链接:代码下载
转载请注明出处:周木水的CSDN博客 http://blog.csdn.net/zhoumushui
我的GitHub:周木水的GitHub https://github.com/zhoumushui