设置登录注册页面输入密码时,监听密码显示隐藏;
代码里面;在oncreate()方法里面调用;还需要先定义一个全局变量:private boolean isHidden = true;
private void setchck_password() { // 设置第一次输入密码未不可见状态 login_password.setTransformationMethod(PasswordTransformationMethod .getInstance()); // 设置CheckBox监听 check_password .setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // isHidden = arg1; if (isHidden) { // 设置EditText文本为可见的 login_password .setTransformationMethod(HideReturnsTransformationMethod .getInstance()); } else { // 设置EditText文本为隐藏的 login_password .setTransformationMethod(PasswordTransformationMethod .getInstance()); } isHidden = !isHidden; login_password.postInvalidate(); // 切换后将EditText光标置于末尾 CharSequence charSequence = login_password.getText(); if (charSequence instanceof Spannable) { Spannable spanText = (Spannable) charSequence; Selection.setSelection(spanText, charSequence.length()); } } }); }
在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:background="@drawable/logging_image" android:orientation="vertical" tools:context=".LoggingActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" > <TextView android:id="@+id/back" android:layout_width="50px" android:layout_height="50px" android:layout_margin="20px" android:background="@drawable/bns_search_del" android:textColor="#fff" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icons" /> </LinearLayout> <LinearLayout android:id="@+id/a" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/image" android:layout_margin="40px" > <ImageView android:id="@+id/image_phone1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/contact_person" /> <EditText android:id="@+id/login_phone" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="10" android:background="#fff" android:hint="请输入手机号" android:padding="15px" /> </LinearLayout> <LinearLayout android:id="@+id/c" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="40px" > <ImageView android:id="@+id/image_phone2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/contact_person" /> <EditText android:id="@+id/code" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="10" android:background="#fff" android:hint="请输入用户名" android:padding="15px" /> </LinearLayout> <LinearLayout android:id="@+id/b" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="40px" > <ImageView android:id="@+id/image_phone3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/contact_person" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="10" > <EditText android:id="@+id/login_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:hint="请输入密码" android:inputType="textPassword" android:padding="15px" /> <CheckBox android:id="@+id/check_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="15dp" android:background="@drawable/logging_checkbox" android:button="@null" /> </RelativeLayout> </LinearLayout> <Button android:id="@+id/login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="40px" android:background="#f90" android:text="注册" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20px" android:gravity="right" android:text="注册即视为同意Help_me《隐私条款》" android:textColor="#fff" android:textSize="20px" /> </LinearLayout>