1. 程式人生 > >QQ登入案例

QQ登入案例

1.QQ登入對應的佈局檔案activity_main.xml:

2.建立工具類,使用SharedPreferences儲存資料,編寫一個Utils類,用於實現QQ號碼和密碼的儲存與獲取功能:


3.MainActivity.java實現當用戶輸完QQ號碼和密碼之後,選擇記住密碼,單機登入按鈕時呼叫Utils.java.saveUserInfo()方法儲存QQ密碼:


4.結果圖:


5.activity_main中的程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.bzu.cn.qqlogin.MainActivity">

  <ImageView
      android:id="@+id/iv_head"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="40dp"
      android:src="@drawable/dddss"
      />
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/iv_head"
        android:layout_margin="10dp"
        android:background="#ffffff"
        android:orientation="vertical">
        <RelativeLayout
            android:id="@+id/rl_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">
            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="賬戶"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/et_number"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/tv_name"
                android:background="@null"
                />

        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#E6E6E6"
            />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rl_userpsw"
            android:layout_margin="10dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tv_psw"
                android:layout_centerVertical="true"
                android:text="密碼"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/et_password"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/tv_psw"
                android:background="@null"/>
        </RelativeLayout>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_login"
        android:layout_below="@+id/layout"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:background="#3c8dc4"
        android:text="登入"
        android:textColor="#ffffff"
        />

</RelativeLayout>
6.Utils.java中的程式碼:
package cn.edu.bzu.cn.qqlogin;

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

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

/**
 * Created by Administrator on 2017/6/9.
 */

public class Utils {
    public static boolean saveUserInfo(Context context, String number, String password){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor edit=sp.edit();
        edit.putString("username",number);
        edit.putString("pwd",password);
        edit.commit();

        return true;
    }
    public static Map<String,String> getUserInfo(Context context){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        String number =sp.getString("username",null);
        String password=sp.getString("pwd",null);
        Map<String,String> userMap=new HashMap<String,String>();
        userMap.put("username",number);
        userMap.put("pwd",password);

        return userMap;
    }
}

7.MainActivity.java中的程式碼:
package cn.edu.bzu.cn.qqlogin;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity  {
    private EditText etNumber;
    private EditText etPassword;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       initView();
        Map<String,String> userInfo=Utils.getUserInfo(this);
        if(userInfo!=null){
            etNumber.setText(userInfo.get("number"));
            etPassword.setText(userInfo.get("password"));
        }
    }

    private void initView() {
        etNumber= (EditText) findViewById(R.id.et_number);
        etPassword= (EditText) findViewById(R.id.et_password);
        btn_login= (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String number=etNumber.getText().toString().trim();
                String password=etPassword.getText().toString().trim();
                if(TextUtils.isEmpty(number)){
                    Toast.makeText(MainActivity.this,"請輸入QQ號碼",Toast.LENGTH_SHORT).show();
                    return;

                }
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(MainActivity.this,"請輸入密碼",Toast.LENGTH_LONG).show();
                    return;

                }
                Toast.makeText(MainActivity.this,"登入成功",Toast.LENGTH_SHORT).show();
                Log.i("MainActivity","請記住密碼:"+number+","+password);

                boolean isSaveSuccess=Utils.saveUserInfo(MainActivity.this,number,password);
                if(isSaveSuccess){
                    Toast.makeText(MainActivity.this,"儲存成功",Toast.LENGTH_LONG).show();

                }else {
                    Toast.makeText(MainActivity.this,"儲存失敗",Toast.LENGTH_LONG).show();

                }
            }
        });
    }
}