以SP方式存储用户名

layout代码:

<?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.my.testapp.Test_Delu">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/et_1"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:" />
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/et_2"
            android:inputType="textPassword"/>

    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:onClick="bt2_onClick"/>
    </LinearLayout>
View Code

Activity代码:

package com.example.my.testapp;

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

public class Test_Delu extends AppCompatActivity {

    EditText et_1,et_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test__delu);
        et_1=(EditText)findViewById(R.id.et_1);
        et_2=(EditText)findViewById(R.id.et_2);
        SharedPreferences sd=getSharedPreferences("用户名", MODE_PRIVATE);

        String c=sd.getString("用户名",null);

            et_1.setText(c);


    }
    public void bt2_onClick(View v)
    {
        EditText et_1=(EditText)findViewById(R.id.et_1);
        EditText et_2=(EditText)findViewById(R.id.et_1);
        String a=et_1.getText().toString();
        String b=et_2.getText().toString();

        if (a==null||a.trim().length()==0||b==null||b.trim().length()==0)
        {
            Toast.makeText(Test_Delu.this, "请正确输入用户名或密码", Toast.LENGTH_SHORT).show();
        }
        else
        {
            SharedPreferences sp=getSharedPreferences("用户名", MODE_APPEND);
            SharedPreferences.Editor editor=sp.edit();

            editor.putString("用户名",a);
            editor.commit();
            Toast.makeText(Test_Delu.this, "登录成功。", Toast.LENGTH_SHORT).show();
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/beens/p/5520610.html