Android输入密码时显示与隐藏
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:58
在登录或者注册的时刻,有些软件中,需求请求暗码可以查看和隐蔽,其实实现起来也很简单。
[img]http://img.blog.csdn.net/20150105175020144?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGVuZ2d1b3hpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
起首定义构造:
<LinearLayout android:layout_width="fill_parent" android:layout_height="50.0dip" android:layout_marginLeft="15dip" android:layout_marginRight="15dip" android:background="@drawable/update_password_item_bg_bottom" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="right|center_vertical" android:paddingLeft="5.0dip" android:text="@string/login_pwd" android:textColor="@android:color/black" android:textSize="16.0sp" /> <EditText android:id="@+id/my_lottery_login_page_password" android:layout_width="0px" android:layout_height="fill_parent" android:layout_marginLeft="5.0dip" android:layout_marginRight="5.0dip" android:layout_weight="3" android:background="@android:color/white" android:hint="@string/login_imput_pwd" android:inputType="textPassword" android:maxLength="20" android:paddingLeft="5.0dip" android:singleLine="true" android:textSize="16.0sp" /> <Button android:id="@+id/my_lottery_login_page_show_password" android:layout_width="45dip" android:layout_height="30dip" android:layout_marginRight="3dip" android:background="@drawable/btn_yangzhengma_selector" android:text="显示" android:textColor="#454545" android:textSize="14sp" /> </LinearLayout>
然后监听那个按钮。在java代铝闼楝定义一个标记
private Boolean showPassword = true; if(showPassword){//显示暗码 showPassword = !showPassword; login_show_password.setText("隐蔽"); login_page_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); login_page_password.setSelection(login_page_password.getText().toString().length()); }else{//隐蔽暗码 showPassword = !showPassword; login_show_password.setText("显示"); login_page_password.setTransformationMethod(PasswordTransformationMethod.getInstance()); login_page_password.setSelection(login_page_password.getText().toString().length()); }