標籤:android 驗證碼 鎖定edittext 隨機驗證碼
昨天寫了個小Demo,實現了隨機產生驗證碼,和鎖定EditText兩個小功能,先看一下:
鎖定EditText在我們不需要使用者編輯EditText內容的時候可以用到,實現還是很簡單的,一行代碼:
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
Android鎖定EditText內容和隨機產生驗證碼