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>

    路漫漫其修远兮 吾将上下而求索
  • 相关阅读:
    div中子div在firefox ie 水平居中对齐
    Access数据库自定义连接字符串(详细有图百度文档)
    ASP页面乱码
    国内开源asp.netCMS汇总
    sql 2005
    卡巴斯基授权key导入方式方法及其导入key基本原理
    关于SqlServer服务无法启动的症状分析和解决方法
    js下流媒体的在线播放
    用JS+vml作三维报表
    了解嵌入式数据库(sqlite,firebird)
  • 原文地址:https://www.cnblogs.com/hudabing/p/3741226.html
Copyright © 2011-2022 走看看