您的位置:首页 > 移动开发 > Android开发

Android开发之显示输入密码功能

2017-07-19 10:13 423 查看
       实训的项目中,在登录的界面中用到了是否显示密码的功能。实现这一效果的方法有两种:

           第一种使用checkbox的默认样式,设置选中或不选中状态;

           第二种使用两张图片进行切换来代表选中或不选中状态;

   以第一种为例:

         效果图如下:

               不选中状态:

          


               选中状态:

          


      具体java实现代码:

         

       package zqq.trys.text;
       import android.support.v7.app.AppCompatActivity;

       import android.os.Bundle;

       import android.text.Editable;

       import android.text.InputType;

       import android.text.Selection;

       import android.view.Window;

       import android.widget.CheckBox;

       import android.widget.CompoundButton;

       import android.widget.EditText;
       public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
                  EditText et_pwd;

                  CheckBox checkBox;

           @Override

           protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              requestWindowFeature(Window.FEATURE_NO_TITLE); //去标题栏

              setContentView(R.layout.activity_main);

              initView();

              setListener();

          }

          public void initView(){

              et_pwd= (EditText) findViewById(R.id.et_pwd);

              checkBox= (CheckBox) findViewById(R.id.checkBox);

          }

         public void setListener(){

              et_pwd.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);

              checkBox.setOnCheckedChangeListener(this);

         }
        @Override

        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if(b){

                //显示输入内容为普通文本

                 et_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

               

                Editable editable=et_pwd.getText();

               Selection.setSelection(editable,editable.length());

             }else{

               //显示输入内容为密码

               et_pwd.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);

    

               Editable editable=et_pwd.getText();

               Selection.setSelection(editable,editable.length());
             }

       }

   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: