EditText displays plaintext and password, and edittext displays plaintext

Source: Internet
Author: User

EditText displays plaintext and password, and edittext displays plaintext
Layout

<? Xml version = "1.0" encoding = "UTF-8"?> <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: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = "liu. basedemo. mainActivity "> <EditText android: id =" @ + id/etUsername "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: layout_margin =" 20dp "android: hint = "Enter the user name" android: textColor = "#000000" android: textColorHint = "#55000000" android: textSize = "20sp"/> <RelativeLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center_vertical"> <EditText android: id = "@ + id/etPassword" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_margin = "20dp" android: hint = "enter the password" android: inputType = "textPassword" android: textColor = "#000000" android: textColorHint = "#55000000" android: textSize = "20sp"/> <CheckBox android: checked = "false" android: id = "@ + id/cbDisplayPassword" android: layout_width = "50dp" android: layout_height = "50dp" android: layout_alignParentRight = "true" android: layout_centerVertical = "true" android: button = "@ drawable/selector_password"/> </RelativeLayout> </LinearLayout>

Selector

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@mipmap/cb_checked" android:state_checked="true"/>    <item android:drawable="@mipmap/cb_normaled" android:state_checked="false"/></selector>
Method 1
Private void initListener () {mCbDisplayPassword. setOnCheckedChangeListener (new CompoundButton. onCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {Log. d (TAG, "onCheckedChanged:" + isChecked); if (isChecked) {// select the plaintext State Display -- set it to a visible password mEtPassword. setInputType (InputType. TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);} else {// display the password in the default status -- set the text to be written together to take effect InputType. TYPE_CLASS_TEXT | InputType. TYPE_TEXT_VARIATION_PASSWORD mEtPassword. setInputType (InputType. TYPE_CLASS_TEXT | InputType. TYPE_TEXT_VARIATION_PASSWORD );}}});}
Method 2
Private void initListener () {mCbDisplayPassword. setOnCheckedChangeListener (new CompoundButton. onCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {Log. d (TAG, "onCheckedChanged:" + isChecked); if (isChecked) {// select the plaintext State Display -- set it to a visible password // mEtPassword. setInputType (InputType. TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);/*** second type */mEtPassword. setTransformationMethod (HideReturnsTransformationMethod. getInstance ();} else {// display the password in the default status -- set the text to be written together to take effect InputType. TYPE_CLASS_TEXT | InputType. TYPE_TEXT_VARIATION_PASSWORD // mEtPassword. setInputType (InputType. TYPE_CLASS_TEXT | InputType. TYPE_TEXT_VARIATION_PASSWORD);/*** type 2 */mEtPassword. setTransformationMethod (PasswordTransformationMethod. getInstance ());}}});}

 

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.