数据存储课后作业

package com.example.wang.myapplication;

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

public class ZuoyeActivity5 extends AppCompatActivity {

    EditText user_name;
    EditText user_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zuoye5);
        user_name=(EditText)findViewById(R.id.user_name);
        user_password=(EditText)findViewById(R.id.user_password);

        SharedPreferences sharedPreferences1 =getSharedPreferences("cunchu",MODE_APPEND);
        String string = sharedPreferences1.getString("用户名", "未添加");
        Log.e("TAG","string="+string);
        user_name.setText(string);
    }
    public  void bt1_OnClick(View v)
    {
        String username=user_name.getText().toString();
        String userpassword=user_password.getText().toString();


            SharedPreferences sharedPreferences=getSharedPreferences("cunchu", MODE_APPEND);

            SharedPreferences.Editor editor=sharedPreferences.edit();

            editor.putString("用户名",username);
            editor.putString("用户密码", userpassword);

            editor.commit();
            Log.e("TAG","username="+username);
            Log.e("TAG", "userpassword="+userpassword);


    }
    public  void bt2_OnClick(View v)
    {
       finish();
    }
}
java
<?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"
    tools:context="com.example.wang.myapplication.ZuoyeActivity5"
    android:orientation="vertical">
    
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
        android:id="@+id/user_name"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:inputType="numberPassword"
        android:id="@+id/user_password"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登陆"
            android:onClick="bt1_OnClick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="关闭"
            android:onClick="bt2_OnClick"/>
    </LinearLayout>

</LinearLayout>
xml

posted @ 2016-05-23 17:01  1011042043  阅读(99)  评论(0编辑  收藏  举报