在登录或者注册的时候,有些软件中,需求要求密码可以查看和隐藏,其实实现起来也很简单。
首先定义布局:
<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>
private Boolean showPassword = false; 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()); }
原文地址:http://blog.csdn.net/lengguoxing/article/details/42425833