<?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:orientation="vertical" tools:context="com.hanqi.zuoyee.cunchu_denglu"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" android:id="@+id/et1"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:text="登录" android:onClick="b1"/> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:text="注册"/> </LinearLayout> </LinearLayout>
layout文件
package com.hanqi.zuoyee; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; public class cunchu_denglu extends AppCompatActivity { EditText et; String zh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cunchu_denglu); et=(EditText)findViewById(R.id.et1); SharedPreferences s=getSharedPreferences("a",MODE_PRIVATE); zh=s.getString("zhanghao",null); if(zh!=null){ et.setText(zh); } } public void b1(View v){ SharedPreferences s=getSharedPreferences("a", MODE_PRIVATE); SharedPreferences.Editor editor = s.edit(); zh=et.getText().toString(); if(zh.trim().length()==0){ return; } editor.putString("zhanghao",zh); editor.commit(); } }