欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-03-19 09:46:23|栏目:Android代码|点击:

在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());

        }

      }
    });

  }
}

效果展示:


上一篇:详解如何在Flutter中集成华为认证服务

栏    目:Android代码

下一篇:Android实现支付宝6位密码输入界面

本文标题:Android中实现密码的隐藏和显示的示例

本文地址:http://www.codeinn.net/misctech/83947.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有