保存用户名记住密码

MainActivity:

package com.example.unit01b_shared_demo03;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class MainActivity extends Activity {
    private EditText phoneEdit, pwdEdit;
    private CheckBox checkBox;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取SharedPreferences对象
        sp = getSharedPreferences("user", MODE_PRIVATE);
        // 找控件
        phoneEdit = (EditText) findViewById(R.id.phone_edit);
        pwdEdit = (EditText) findViewById(R.id.pwd_edit);
        checkBox = (CheckBox) findViewById(R.id.mycheck);
        // checkbox监听事件;
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if (isChecked) {// 勾选记住密码,保存用户名密码
                    if (getEditText(phoneEdit) != null
                            && getEditText(pwdEdit) != null) {
                        // 两个输入框都有值;
                        sp.edit().putString("phone", getEditText(phoneEdit))
                                .commit();
                        sp.edit().putString("pwd", getEditText(pwdEdit))
                                .commit();
                    }
                }
            }
        });
        getData();
    }

    // 获取edittext的值;
    private String getEditText(EditText edit) {
        String text = edit.getText().toString().trim();
        if (isNotNull(text)) {
            return text;
        }
        return null;
    }

    // 首先进入此界面,从SharedPreferences里面获取用户名和密码,如果有值,显示到界面上;
    private void getData() {
        String phone = sp.getString("phone", "");
        String pwd = sp.getString("pwd", "");
        if (isNotNull(phone) && isNotNull(pwd)) {// 不为空,显示到edittext;
            phoneEdit.setText(phone);
            pwdEdit.setText(pwd);
            checkBox.setChecked(true);
        }
    }

    // !!!!!!!!!
    private boolean isNotNull(String text) {
        if (text != null && !text.equals("")) {// 不为空,并且不是空字符串;
            return true;
        }
        return false;
    }
}

xml布局

<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"
    android:gravity="center_horizontal"
    tools:context="com.example.unit01b_shared_demo03.MainActivity" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp"
    android:gravity="center_vertical"
    >
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="手机号" />
     <EditText
         android:id="@+id/phone_edit"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="请输入您的手机号"
         android:background="@drawable/edit_outlin"
         android:padding="5dp"
         android:layout_margin="5dp"
         />
</LinearLayout>
   
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp"
    android:gravity="center_vertical"
    >
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:text="密码" />
     <EditText
         android:id="@+id/pwd_edit"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="请输入您的密码"
         android:background="@drawable/edit_outlin"
         android:padding="5dp"
         android:layout_margin="5dp"
         />
</LinearLayout>
   <CheckBox
       android:id="@+id/mycheck"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="记住密码"
       />
<Button
    android:id="@+id/login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="登录"
    android:layout_margin="10dp"
    />
</LinearLayout>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值