当前位置: 移动技术网 > 移动技术>移动开发>Android > Android中实现密码的隐藏和显示的示例

Android中实现密码的隐藏和显示的示例

2019年07月24日  | 移动技术网移动技术  | 我要评论

在android开发中,需要密码的隐藏和显示,下面就和大家分享一下使用方法:

xml代码:

    <linearlayout 
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:background="@color/white"
      android:orientation="horizontal" >

      <textview
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="新密码"
        android:textcolor="@color/black"
        android:textsize="18dp"
        android:gravity="center_vertical"
        android:layout_marginleft="15dp"/>

      <edittext
        android:id="@+id/newpassword"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_marginleft="10dp"
        android:inputtype="textpassword"
        android:hint="请设置登录密码"
        android:background="@null"/>

      <checkbox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginright="15dp"
        android:textsize="16dp"
        android:text="显示"
        />

    </linearlayout>

隐藏图标代码

android:button="@null"

java代码:

/**
 * created by fby on 2017/9/11.
 */

public class chargepsdactivity extends activity {

  private edittext edittext;
  private checkbox checkbox;

  @override
  protected void oncreate(@nullable bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_chargepsd);

    edittext = (edittext) findviewbyid(r.id.newpassword);
    checkbox = (checkbox) findviewbyid(r.id.checkbox);

    checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
      @override
      public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {

        if(ischecked){
          //如果选中,显示密码
          edittext.settransformationmethod(hidereturnstransformationmethod.getinstance());
        }else{
          //否则隐藏密码
          edittext.settransformationmethod(passwordtransformationmethod.getinstance());

        }

      }
    });

  }
}

效果展示:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网