安卓保存与回显账号密码

安卓保存与回显账号密码

	使用SharedPreferences存放变量

/**
 * 保存与回显账号密码(SharedPreferences)
 */
public class MainActivity extends AppCompatActivity {
    
    private EditText phone;
    private EditText psw;
    
    private CheckBox check;
    private SharedPreferences sharedPreferences;
    
    private String TAG = "MainActivity";
    private String SP_PHONE= "sp_phone";
    private String SP_PSW= "sp_psw";
    private String SP_IS_REMEMBER ="SP_IS_REMEMBER" ;

    private boolean IsChecked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        initUI();
        
        // 初始化数据
        initData();
    }

    private void initData() {
        // 实例化sharedPreferences
        if(sharedPreferences==null){
             sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        
        // 回显数据
        phone.setText(sharedPreferences.getString(SP_PHONE,""));
        psw.setText(sharedPreferences.getString(SP_PSW,""));
        
        IsChecked = sharedPreferences.getBoolean(SP_IS_REMEMBER,false);
        check.setChecked(IsChecked);
    }

    private void initUI() {
        // 获取电话输入框
        phone = findViewById(R.id.phone);
        
        // 文本改变之后记录用户电话
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                
                if(IsChecked){
                    if(sharedPreferences==null){
                        // 实例化SharedPreferences对象
                        sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
                    }
                    // 实例化SharedPreferences对象的编辑者对象
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putString(SP_PHONE,phone.getText().toString());
                    edit.commit();
                }
                
            }
        });
        
        // 获取密码输入框
        psw = findViewById(R.id.psw);

        // 文本改变之后记录用户密码
        psw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                
                if(IsChecked){
                    if(sharedPreferences==null){
                        // 实例化SharedPreferences对象
                        sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
                    }

                    // 实例化SharedPreferences对象的编辑者对象
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putString(SP_PSW,psw.getText().toString());    
                    edit.commit();
                }
                
            }
        });
        
        // 获取多选按钮
        check = findViewById(R.id.rem);
        check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i(TAG,"状态为:"+isChecked);
                IsChecked = isChecked;
                
                if(isChecked){
                    // 实例化SharedPreferences对象
                    sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
                    
                    if(sharedPreferences==null){
                        sharedPreferences = getApplicationContext().getSharedPreferences("config", Context.MODE_PRIVATE);
                    }
                    
                    // 实例化SharedPreferences对象的编辑者对象
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    // 存储数据
                    edit.putString(SP_PHONE,phone.getText().toString());
                    edit.putString(SP_PSW,psw.getText().toString());
                    edit.putBoolean(SP_IS_REMEMBER, isChecked);
                    
                    edit.commit();
                    
                }
            }
        });
    }
    
}

页面代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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/phone"
        android:inputType="text"
        android:hint="电话"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <EditText
        android:id="@+id/psw"
        android:inputType="textPassword"
        android:hint="密码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <CheckBox
        android:id="@+id/rem"
        android:text="记住密码"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        />

</LinearLayout>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值