android实现登录保存账号密码 隐藏显示密码CheckBox

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="100dp"
        android:text="用户名"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="100dp"
        android:hint="在这里输入你的用户名" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:text="密码"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:hint="在这里输入你的密码"
        android:inputType="textPassword" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+ideckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:checked="false"
        android:text="记住用户名" />

    <CheckBox
        android:id="@+ideckBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_weight="1"
        android:checked="false"
        android:text="记住密码" />
    <CheckBox
        android:id="@+id/cb_showPassword"
        android:layout_marginRight="30dp"
        android:layout_below="@id/editPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="显示密码" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="80dp"
        android:text="登陆" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:text="取消" />
</LinearLayout>
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button login, cancel;
    private CheckBox checkBox, checkBox2;
    private EditText editName, editPassword;
    //声明一个SharedPreferences对象和一个Editor对象
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        login = (Button) findViewById(R.id.login);
        cancel = (Button) findViewById(R.id.cancel);
        editName = (EditText) findViewById(R.id.editName);
        editPassword = (EditText) findViewById(R.id.editPassword);
        checkBox = (CheckBox) findViewById(R.id.checkBox);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        //获取preferences和editor对象
        preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        editor = preferences.edit();
cb_showPassword.setOnClickListener(new View.OnClickListener() {
            boolean isPwdVisible = false;

            @Override
            public void onClick(View v) {
                if (isPwdVisible == false) {
                    //显示密码方法一
                    HideReturnsTransformationMethod method2 = HideReturnsTransformationMethod.getInstance();
                    editPassword.setTransformationMethod(method2);
                    isPwdVisible = !isPwdVisible;
                } else {
                    //隐藏密码方法一
                    PasswordTransformationMethod method1 = PasswordTransformationMethod.getInstance();
                    editPassword.setTransformationMethod(method1);
                    isPwdVisible = !isPwdVisible;
                }
                //切换后将EditText光标置于末尾
                editPassword.setSelection(editPassword.getText().toString().length());

            }
        });
    /*
    启动程序时首先检查sharedPreferences中是否储存有用户名和密码
    若无,则将checkbox状态显示为未选中
    若有,则直接中sharedPreferences中读取用户名和密码,并将checkbox状态显示为已选中
    这里getString()方法需要两个参数,第一个是键,第二个是值。
    启动程序时我们传入需要读取的键,值填null即可。若有值则会自动显示,没有则为空。
     */
        String name = preferences.getString("userName", null);
        if (name == null) {
            checkBox.setChecked(false);
        } else {
            editName.setText(name);
            checkBox.setChecked(true);
        }
        String password = preferences.getString("userPassword", null);
        if (password == null) {
            checkBox2.setChecked(false);
        } else {
            editPassword.setText(password);
            checkBox2.setChecked(true);
        }

        //为login和cancel设置监听事件
        login.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //判断用户是进行的是登陆操作还是取消操作
        switch (v.getId()) {
            case R.id.login:
                String name = editName.getText().toString().trim();
                String password = editPassword.getText().toString().trim();
                //验证用户名和密码,若为admin-123456即可登录
                if (name.equals("admin") && password.equals("123456")) {
                    if (checkBox.isChecked()) {
                        //如果用户选择了记住用户名
                        //将用户输入的用户名存入储存中,键为userName
                        editor.putString("userName", name);
                        editor.commit();
                    } else {
                        //否则将用户名清除
                        editor.remove("userName");
                        editor.commit();
                    }
                    if (checkBox2.isChecked()) {
                        //如果用户选择了记住密码
                        //将用户输入的密码存入储存中,键为userName
                        editor.putString("userPassword", password);
                        editor.commit();
                    } else {
                        //否则将密码清除
                        editor.remove("userPassword");
                        editor.commit();
                    }
                    //提示登陆成功
                    Toast.makeText(this, "login success", Toast.LENGTH_SHORT).show();
                } else {
                    //若登陆不成功,则将错误的用户名和密码清除,并提示登陆失败
                    editor.remove("userName");
                    editor.remove("userPassword");
                    editor.commit();
                    Toast.makeText(this, "login failed", Toast.LENGTH_SHORT).show();
                }
                break;
            //若用户选择了取消,则直接退出登录
            case R.id.cancel:
                finish();
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值