zoukankan      html  css  js  c++  java
  • Android学习笔记之打钩显示输入的密码

    利用EditText作为密码输入框是个不错的选择(只需设置输入类型为textPassword即可),保密且无需担心被盗取。但有时用户也不知道自己输入的是否正确,这时就应该提供一个“显示密码”的复选框,让用户控制密码框的显示方式,看到自己输入的密码,然后必要时再关闭此功能。

    本程序就使用了一个CheckBox组件,让用户选择是否显示明文密码,程序效果如下图所示:

    Activity程序如下所示:

    public class MainActivity extends Activity 
    {
     private EditText password=null;
     private CheckBox check=null;

     @Override
     protected void onCreate(Bundle savedInstanceState) 
     {  
      super.onCreate(savedInstanceState);
      super.setContentView(R.layout.activity_main);
      this.password=(EditText)super.findViewById(R.id.password);
      this.check=(CheckBox)super.findViewById(R.id.check);
      //为check设置监听选项,控制密码框的显示方式
      this.check.setOnClickListener(new OnClickListener()
      { 
       @Override
       public void onClick(View v) 
       {
        if(check.isChecked())
        {
         //设置密码可见
         password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
        else
        {
            //设置密码隐藏
         password.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
       }
      });
     }

    }

    布局文件如下所示:

    <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:orientation="vertical"
        tools:context=".MainActivity" >

        <EditText
            android:id="@+id/password"
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="40sp"
            android:hint="请输入密码:" />
        
        <CheckBox
            android:id="@+id/check"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="显示密码" />

    </LinearLayout>

    路漫漫其修远兮 吾将上下而求索
  • 相关阅读:
    配置apache+php环境详解
    美剧推荐之《行尸走肉》
    代码轻松实现wordpress彩色标签云
    php配置支持mysql解决本地安装wordpress问题
    struts2通配符和动态方法调用
    struts2文件上传1
    struts2入门
    jQuery3
    ADT20安装报错
    Android入门
  • 原文地址:https://www.cnblogs.com/hudabing/p/3741226.html
Copyright © 2011-2022 走看看