Hide and show input passwords in Android layout

Source: Internet
Author: User
Tags gettext

<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 :p addingtop= "@dimen/activity_vertical_margin" android:paddingbottom= "@dimen/activity_vertical_margin" Android:o rientation= "Vertical" tools:context= ".        Mainactivity "> <linearlayout android:orientation=" Horizontal "android:layout_width=" Match_parent "            android:layout_height= "Wrap_content" > <textview android:layout_width= "wrap_content"        android:layout_height= "Wrap_content" android:text= "Name:" android:id= "@+id/textview"/>            <edittext android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:id= "@+id/tv1"           android:layout_weight= "0.91"/> </LinearLayout> <linearlayout android:orientation= "Hor Izontal "android:layout_width=" match_parent "android:layout_height=" Wrap_content "android:weightsum="            1 "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:text= "Password:" android:id= "@+id/textview1"/> <edittext Andro            Id:layout_width= "Match_parent" android:layout_height= "wrap_content" android:id= "@+id/password"        Android:inputtype= "Textpassword"/> </LinearLayout> <checkbox android:id= "@+id/check" Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "Show password"/> <L Inearlayout android:orientation= "Horizontal" android:layout_width= "Match_parent" Android:layout_heigh        t= "Wrap_content"> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content"            android:text= "Login" android:id= "@+id/btnl" android:layout_weight= "1"/> <textview            Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "" android:layout_weight= "android:id=" @+id/textview2 "/> <button android:la Yout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Exit" Android: Id= "@+id/button2" android:layout_weight= "1" android:layout_toendof= "@+id/textview2" Andro id:layout_gravity= "Right"/> </LinearLayout></LinearLayout>

Package Com.banana.k08.home;import Android.app.activity;import Android.os.bundle;import Android.text.method.hidereturnstransformationmethod;import Android.text.method.PasswordTransformationMethod; Import Android.view.menu;import android.view.menuitem;import Android.view.view;import Android.widget.Button;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.edittext;import    Android.widget.toast;public class Mainactivity extends Activity {private EditText Id,password;    Private CheckBox CB;    Private Button btn1;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        id = (EditText) Findviewbyid (R.ID.TV1);        Password = (EditText) Findviewbyid (R.id.password);        CB = (CheckBox) Findviewbyid (R.id.check);      Set Select whether to show Password Cb.setoncheckedchangelistener (new Compoundbutton.oncheckedchangelistener () {@Override      public void OnCheckedChanged (Compoundbutton Compoundbutton, Boolean b) {if (cb.ischecked ()) {                Password.settransformationmethod (Hidereturnstransformationmethod.getinstance ());                } else{Password.settransformationmethod (Passwordtransformationmethod.getinstance ());        }            }        }); Findviewbyid (R.ID.BTNL). Setonclicklistener (New View.onclicklistener () {@Override public void onclic                K (View v) {String name = Id.gettext (). toString ();                String pass = Password.gettext (). toString (); if (name.equals ("admin") && pass.equals ("admin")) {Toast.maketext (Getapplicationcontext (), "Log into                Toast.length_short). Show ();                } else {Toast.maketext (Getapplicationcontext (), "Login failed, please reenter", toast.length_short). Show ();    }            }        }); }    }


Hide and show input passwords in Android layout

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.