Android lock EditText content and randomly generated verification code

Source: Internet
Author: User

Yesterday wrote a small demo, realize the random generation of verification code, and lock EditText Two small functions, first look:



Lock EditText can be used when we don't need the user to edit EditText content, the implementation is very simple, a line of code:

Etlock.setenabled (FALSE);

Randomly generated verification code, mainly using the random function, as well as the logic to turn the view into bitmap, there is no difficulty, paste the code below, for the needs of friends 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    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 V Iew) {if (view = = Btnlock) {if (etlock.isenabled ()) {etlock.setenabled (FAL                    SE);                Btnlock.settext ("unlock");                    } else {etlock.setenabled (true);                Btnlock.settext ("lock"); }} else if (view = = Btncheck) {if (Etcheck.gettext (). toString ()! = null && etcheck.get                    Text (). toString (). Trim (). Length () > 0) {tvcheck.setvisibility (view.visible);                        if (Numstr.equals (Etcheck.gettext (). toString ())) {Tvcheck.settext ("input correct");                    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, 0, 0, Matrixa, true);        Ivnuma.setimagebitmap (Bmnuma);        Num 2 Matrix MATRIXB = new Matrix ();        Matrixb.reset ();        Matrixb.setrotate (Randomangle ());        Bitmap bmnumb = Bitmap.createbitmap (Getbitmapfromview (tvhideb, 0, 0, MATRIXB, true);        Ivnumb.setimagebitmap (BMNUMB);        Num 3 Matrix MATRIXC = new Matrix ();        Matrixc.reset ();        Matrixc.setrotate (Randomangle ());        Bitmap BMNUMC = Bitmap.createbitmap (Getbitmapfromview (tvhidec, 0, 0, MATRIXC, true);        Ivnumc.setimagebitmap (BMNUMC);        Num 4 Matrix matrixd = new Matrix ();        Matrixd.reset ();        Matrixd.setrotate (Randomangle ());        Bitmap BMNUMD = Bitmap.createbitmap (Getbitmapfromview (tvhided, 0, 0, Matrixd, true); Ivnumd.setimagebItmap (BMNUMD);    } public int Randomangle () {return * (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: not clear int randomcolorid = new Random (). Nextint (6);    return Colorarray[randomcolorid]; } public static Bitmap Getbitmapfromview (view view, int width, int height) {int widthspec = View.MeasureSpec.ma        Kemeasurespec (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 Actio        n 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 would//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:paddi ngleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" Android: paddingtop= "@dimen/activity_vertical_margin" android:paddingbottom= "@dimen/activity_vertical_margin" Android:o rientation= "Vertical" tools:context= ". Main "> <textview android:layout_width=" wrap_content "android:text=" Lock EditText content: "Android:la yout_height= "Wrap_content"/> <linearlayout android:layout_width= "Fill_parent" Android:layout_heigh            t= "wrap_content" android:orientation= "horizontal" > <edittext android:id= "@+id/etlock" Android:layout_width= "200DP" android:layout_height= "Wrap_content"/> <button Android Oid:id= "@+id/btnlock"Android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:text=" Lock "/> </LinearLayout> <textview android:layout_margintop=" 50DP "android:layout_width=" Wra P_content "android:layout_height=" wrap_content "android:text=" randomly generates 4-bit verification code: "/> <linearlayout A        Ndroid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <linearlayout android:id= "@+id/layverify" android:orientation= "Horizontal" Android                Oid: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" an                Droid:layout_width= "70DP" android:layout_height= "70DP" android:textsize= "30DP" android:gravity= "center" android:visibility= "Gone"/> </LinearLayout> <linearla Yout android:layout_width= "wrap_content" android:orientation= "Horizontal" android:layout_ height= "Wrap_content"; <imageview android:id= "@+id/ivnuma" android:layout_width= "50DP" Android:la yout_height= "70DP"/> <imageview android:id= "@+id/ivnumb" Android:layout_wi Dth= "50DP" android:layout_height= "70DP"/> <imageview android:id= "@+id/ivnu                MC "android:layout_width=" 50DP "android:layout_height=" 70DP "/> <imageview Android:id= "@+id/ivnumd" android:layout_width= "50DP" android:layout_height= "7 0DP "/> </LinearLayout> </LinearLayout> <linearlayout android:layout_width=" Fill_par            Ent "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=" Results "Android    : visibility= "Gone" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> </LinearLayout> <button android:id= "@+id/btncheck" android:layout_width= "Wrap_content" a ndroid:layout_height= "Wrap_content" android:text= "OK"/></linearlayout>

Have a question Welcome to Exchange ~

Just upload the code and write it in Android studio. Link: code Download

Reprint Please specify source: Zhou Mushi's csdn blog Http://blog.csdn.net/zhoumushui

My github: Zhou Mushi's GitHub Https://github.com/zhoumushui






Android Lock EditText content and randomly generated verification code

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.