QQ登录带数据保存基础案例

package com.glandroid.injune11;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{

    private EditText name;
    private EditText password;
    private CheckBox check;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        init();
        sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        boolean ischeck = sp.getBoolean("ischeck", false);
        String sname = sp.getString("name", null);
        String spassword = sp.getString("password", null);
        if(ischeck)
        {
            name.setText(sname);
            password.setText(spassword);
        }
    }

    private void init() {
        name = (EditText) findViewById(R.id.etname);
        password = (EditText) findViewById(R.id.etpassword);
        findViewById(R.id.login).setOnClickListener(this);
        check = (CheckBox) findViewById(R.id.check);
    }

    

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                String tname=name.getText().toString();
                String tpassword=password.getText().toString();
                SharedPreferences.Editor edit = sp.edit();
                if(tname.equals("admin")&&tpassword.equals("123456"))
                {
                    Toast.makeText(this,"登陆成功", Toast.LENGTH_SHORT).show();
                    if(check.isChecked())
                    {

                        edit.putString("name",tname);
                        edit.putString("password",tpassword);
                        edit.putBoolean("ischeck",check.isChecked());
                    }
                    else
                    {
                        edit.clear();
                    }
                    edit.commit();
                }
                else
                {
                    Toast.makeText(this,"登陆失败", Toast.LENGTH_SHORT).show();

                }

                break;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="用户名"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
        <EditText
            android:id="@+id/etname"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="密码"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
        <EditText
            android:id="@+id/etpassword"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

    </LinearLayout>
    <CheckBox
        android:id="@+id/check"
        android:text="保存"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:layout_gravity="center_horizontal"
        android:id="@+id/login"
        android:text="登录"
        android:background="#04ff3a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/qq_41039734/article/details/80728777