Android locks EditText content and randomly generated verification code, androidedittext

Source: Internet
Author: User

Android locks EditText content and randomly generated verification code, androidedittext

I wrote a small Demo yesterday to implement the Random verification code generation and EditText locking functions. Let's take a look at it first:



Locking EditText can be used when users do not need to edit EditText content. The implementation is still very simple, with a line of code:

etLock.setEnabled(false);

The Random verification code is generated mainly by using the Random function and converting the View into the Bitmap logic. There is no difficulty. I will paste the code below for your reference:


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 Droid. 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 iv NumA; 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. setOnCl IckListener (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 ("unlock");} else {etLock. setEnabled (true); btnLock. setText ("locked") ;}} 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 ("correct input"); tvCheck. setTextColor (Color. GREEN);} else {tvCheck. setText ("input error"); 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: the int randomColorId = new Random () is not clear (). 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 );}}

Layout file: 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" android: paddingBottom = "@ dimen/plugin" android: orientation = "vertical" tools: context = ". main "> <TextView android: layout_width =" wrap_content "android: text =" lock EditText content: "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 = "locked"/> </LinearLayout> <TextView android: layout_marginTop = "50dp" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "randomly generated 4-digit Verification Code:"/> <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 android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <EditText android: id = "@ + id/etCheck" android: hint = "Verification Code" android: layout_width = "80dp" android: layout_height = "wrap_content"/> <TextView android: id = "@ + id/tvCheck" android: text = "result" 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 = "OK"/> </LinearLayout>

If you have any questions, please contact us ~

Upload the code using Android Studio. Link: Code download

Reprinted please indicate the source: Zhou mu Shui CSDN blog http://blog.csdn.net/zhoumushui

My GitHub: Zhou mu Shui's GitHub https://github.com/zhoumushui






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.