登录界面的记住密码和密码的隐藏、显示

登录界面的记住密码功能

先上布局代码:

 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/login_title"/>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="40dp">
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/shape_1">
    <!--该开源项目是用来轻松实现图片圆形化的共能-->
    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/icon_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="57dp"
        android:layout_marginStart="57dp"
        android:src="@drawable/img_x" />

    <ImageView
        android:id="@+id/edit_view"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/take_picture"
        android:layout_toStartOf="@+id/take_picture"
        android:src="@drawable/ic_stay_primary_portrait_black_24dp" />

    <Button
        android:id="@+id/take_picture"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="17dp"
        android:layout_marginRight="17dp"
        android:text="点击拍照切换头像"
        android:textSize="10sp" />

    <ImageView
        android:id="@+id/take_form_album"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_below="@+id/take_picture"
        android:layout_marginTop="12dp"
        android:layout_toLeftOf="@+id/take_picture"
        android:layout_toStartOf="@+id/take_picture"
        android:src="@drawable/ic_picture"/>

    <Button
        android:id="@+id/take_album"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_toRightOf="@+id/take_form_album"
        android:layout_below="@+id/take_picture"
        android:text="相册选择头像"
        android:textSize="10sp" />
</RelativeLayout>


<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#fff">
</LinearLayout>

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

    <TextView
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_gravity="center_vertical"
        android:text="Username:"/>
    <EditText
        android:id="@+id/username"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"/>

</LinearLayout>

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

    <TextView
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Email:"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/mail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"/>
    <ImageView
        android:id="@+id/mail_Visibility"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_visibility" />
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/remember_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold"
        android:text="Remember password"/>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="30dp">
</LinearLayout>
<Button
    android:id="@+id/login"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@drawable/shape"
    android:text="Login"
    android:textAllCaps="false"
    android:textColor="#0f0e0e"
    android:textSize="18sp"
    android:textStyle="bold" />
</LinearLayout>
效果就如下:

至于逻辑,及先在onCreate()方法中获取sharedPreferences对象,

//通过SharedPreferences储存实现记住密码功能
private SharedPreferences pref;
//获取了SharePreferences对象
pref = PreferenceManager.getDefaultSharedPreferences(this);

然后调用它的getBoolean()方法去获取remember_password这个键值,

//调用SharePreferences的getBoolean()方法获取remember_password这个键值,一开始默认的为false
boolean isRemember = pref.getBoolean("remember_password",false);
if (isRemember){
    //将账号和密码都设置文本框中
    String username = pref.getString("username","");
    String email = pref.getString("mail","");
    usernameEdit.setText(username);
    emailEdit.setText(email);
    //将CheckBox状态设置为勾选
    rememberPass.setChecked(true);
}

接着在登陆成功之后,会调用CheckBoxisChecked()方法来检查复选框是否被选中,

case R.id.login:
    String username = usernameEdit.getText().toString();
    String email = emailEdit.getText().toString();
    editor = pref.edit();
//检查复选框是否被选中
if (rememberPass.isChecked()){

如果被选中的话,只是将remember_password设置为true,然后吧usernameemail对应的只都存入到sharedPreferences文件中并提交,

editor.putBoolean("remember_password",true);
editor.putString("username",username);
editor.putString("mail",email);

如果没有被选中,就简单的调用一下clear()方法,将sharedPreferences文件中的数据清除掉就可以了

 
 
else {
editor .clear() ;
}
editor.apply();
Intent intent = new Intent();
intent.putExtra("username_return",username);
intent.putExtra("email_return",email);
setResult(RESULT_OK,intent);
}

整体的逻辑代码:

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

   


    private CircleImageView circleImageView;
    //创建Uri对象
    private Uri imageUri;

    //调用摄像头选择头像
    private Button take_picture;
    //调用相册进行选择头像
    private Button take_album;

    private static int FLAG = 1;

    private EditText usernameEdit;
    private EditText emailEdit;

    private Button titleButton;
    private Button homeButton;
    private Button loginButton;


    private TextView usernameText;
    private TextView emailText;

    private ImageView show_password;
    private CheckBox rememberPass;

    //通过SharedPreferences储存实现记住密码功能
    private SharedPreferences pref;

    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //调用摄像头选择头像
        take_picture = (Button) findViewById(R.id.take_picture);
        //调用相册进行选择头像
        take_album = (Button) findViewById(R.id.take_album);

        usernameText = (TextView) findViewById(R.id.username);
        emailText = (TextView) findViewById(R.id.mail);

        //获取了SharePreferences对象
        pref = PreferenceManager.getDefaultSharedPreferences(this);

        usernameEdit = (EditText) findViewById(R.id.username);
        emailEdit = (EditText) findViewById(R.id.mail);

        circleImageView = (CircleImageView) findViewById(R.id.icon_image);
        show_password = (ImageView) findViewById(R.id.mail_Visibility);
        rememberPass = (CheckBox) findViewById(R.id.remember_password);
        titleButton = (Button) findViewById(R.id.title_back_1);
        homeButton = (Button) findViewById(R.id.title_home);
        loginButton = (Button) findViewById(R.id.login);
        titleButton.setOnClickListener(this);
        homeButton.setOnClickListener(this);
        loginButton.setOnClickListener(this);
        show_password.setOnClickListener(this);

//        if ()

        //调用SharePreferences的getBoolean()方法获取remember_password这个键值,一开始默认的为false
        boolean isRemember = pref.getBoolean("remember_password",false);
        if (isRemember){
            //将账号和密码都设置文本框中
            String username = pref.getString("username","");
            String email = pref.getString("mail","");
            usernameEdit.setText(username);
            emailEdit.setText(email);
            //将CheckBox状态设置为勾选
            rememberPass.setChecked(true);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_back_1:
                finish();
                break;
            case R.id.title_home:

                break;
            case R.id.login:
                String username = usernameEdit.getText().toString();
                String email = emailEdit.getText().toString();
                editor = pref.edit();
                //检查复选框是否被选中
                if (rememberPass.isChecked()){
                    editor.putBoolean("remember_password",true);
                    editor.putString("username",username);
                    editor.putString("mail",email);
                }else {
                    editor.clear();
                }
                editor.apply();
                Intent intent = new Intent();
                intent.putExtra("username_return",username);
                intent.putExtra("email_return",email);
                setResult(RESULT_OK,intent);
                finish();
                break;
            default:
        }
    }


接下来我们看一下密码的隐藏跟显示功能

隐藏EditText1

editText1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());  

显示EditText1

editText1.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

效果如下:

                                                        


具体的项目可以访问我的GIthub及刚写的一个未完成的项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值