QQ账号密码的保存与读取

UserInfo

package com.example.hello;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 王林强 on 2018/5/9.
 */
//这只是类的描述定义而已,不是申请存储空间(定义对象才是存储空间)
public class userInfo {

    //成员变量


    //
    public static Map<String,String> getuserInfo(Context context){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        String str1=sp.getString("qqnumber","");//拆包裹1
        String str2=sp.getString("password","");//拆包裹2
        Map<String,String> userMap=new HashMap<String,String>();
        userMap.put("num",str1);//放包裹3
        userMap.put("mima",str1);//放包裹4
        return userMap;

    }
    public static  boolean saveUserInfo(Context context,String number,String mima){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("qqnumber",number);
        editor.putString("password",mima);
        editor.commit();
        return true;

    }
}
MainActivity
package com.example.hello;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

EditText edt1;
EditText edt2;
Button button;

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

        edt1=findViewById(R.id.editText4);
        edt2=findViewById(R.id.editText5);
        button=findViewById(R.id.button);
        button.setOnClickListener(this);
        Map<String,String> user=userInfo.getuserInfo(this);
        if(user!=null){
            edt1.setText(user.get("num"));//拆包裹3
            edt2.setText(user.get("mima"));//拆包裹4
        }

    }

    @Override
    public void onClick(View view) {




        String str1=edt1.getText().toString().trim();
        String str2=edt2.getText().toString().trim();
        if (TextUtils.isEmpty(str1)){
            Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_SHORT).show();

        }
        if (TextUtils.isEmpty(str2)){
            Toast.makeText(this,"请输入QQ密码",Toast.LENGTH_SHORT).show();

        }
        Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
        //保存QQ账号和密码
        Boolean b1=userInfo.saveUserInfo(this,str1,str2);
        if(b1) {
            Toast.makeText(this, "保存成功",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "保存失败",Toast.LENGTH_SHORT).show();

        }
    }

}

layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/beijing"
        android:scaleType="centerCrop"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        app:srcCompat="@drawable/hit" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp"
        android:orientation="horizontal"
        android:id="@+id/linearLayout">
        <EditText
            android:id="@+id/editText4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="QQ号/手机号/邮箱"
            android:inputType="textPersonName" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text"
            android:text="注册用户"
            android:textSize="20dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:orientation="horizontal"
        android:id="@+id/linearLayout2">
        <EditText
            android:id="@+id/editText5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="密码"
            android:inputType="textPassword" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="忘记密码" />

    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="登录" />

</RelativeLayout>

转载于:https://my.oschina.net/u/3857245/blog/1812385

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值