登陆

1,布局

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.app3.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名:"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"/>
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密  码:"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/pass"/>
    </LinearLayout>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:id="@+id/reme"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_height="wrap_content">
        <Button
            android:onClick="doClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登陆"
            android:id="@+id/btn_login"/>
        <Button
            android:onClick="doClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="重置"
            android:id="@+id/btn_reset"/>
    </LinearLayout>
</LinearLayout>

2,逻辑

package com.example.app3;

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

public class MainActivity extends AppCompatActivity {
    private EditText et_name,et_pass;
    private Button btn_login,btn_reset;
    private CheckBox cb;
    private SharedPreferences sp;
    private static final int MODEL=MODE_PRIVATE;
    private SharedPreferences.Editor  editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name=(EditText)findViewById(R.id.name);
        et_pass=(EditText)findViewById(R.id.pass);
        btn_login=(Button)findViewById(R.id.btn_login);
        cb=(CheckBox)findViewById(R.id.reme);
        sp=getSharedPreferences("info",MODEL);
        editor=sp.edit();

        //二次登陆
        String s_name=sp.getString("u_name","");
        String s_pass=sp.getString("u_pass","");
        et_name.setText(s_name);
        if(s_pass=="" || s_pass==null)
        {
            cb.setChecked(false);
        }
        else
        {
            et_pass.setText(s_pass);
            cb.setChecked(true);
        }

    }

    public void doClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_login:
            String name=et_name.getText().toString();
                String pass= et_pass.getText().toString();
                editor.putString("u_name",name);
                if(name.equals("zhangsan")&&pass.equals("123"))
                {
                    Toast.makeText(MainActivity.this,"success",Toast.LENGTH_LONG).show();
                   if(cb.isChecked())
                   {

                       editor.putString("u_pass",pass);
                       editor.commit();

                   }
                    else
                   {
                       editor.remove("u_pass");
                   }
                }
                else
                {
                    Toast.makeText(MainActivity.this,"faile",Toast.LENGTH_LONG).show();
                }
                editor.commit();
                break;
            case R.id.btn_reset:
                et_pass.setText("");
                et_name.setText("");
                cb.setChecked(false);
                break;
        }
    }
}

3,显示

原文地址:https://www.cnblogs.com/excellencesy/p/9076758.html