首页 > 代码库 > 小组作业 5月9号

小组作业 5月9号

<?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:id="@+id/activity_main"
    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.a59application.MainActivity">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入姓名"
    android:id="@+id/xm"
    />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入年龄"
        android:id="@+id/nl"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/xr"
            android:text="写入"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/dq"
            android:text="读取"
            />
    </LinearLayout>
</LinearLayout>

java

package com.example.a59application;

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

public class MainActivity extends AppCompatActivity {
    private EditText xm;
    private  EditText nl;
    private Button xr;
    private Button dq;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        xm = (EditText)findViewById(R.id.xm);
        nl=(EditText)findViewById(R.id.nl);
        xr=(Button)findViewById(R.id.xr);
        dq=(Button)findViewById(R.id.dq);
        //按钮监听事件
        xr.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            
        //转换
                String name=xm.getText().toString();
                String age = nl.getText().toString();
                //构造方法
                savaToPrefs(name,age);
                
                //显示
                Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_LONG).show();
            }
            private boolean savaToPrefs(String name,String age) {
                SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
                SharedPreferences.Editor edit  = preferences.edit();
                edit.putString("name",name);
                edit.putString("age",age);
                edit.apply();//显示或者edit.commt()
                return  true;
            }
        });
               //按钮监听事件
        dq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //构造方法
                readFromprefs();
            }
            private void readFromprefs() {
                  //读取
                SharedPreferences preferences1 =getSharedPreferences("data",MODE_PRIVATE);
                String name = preferences1.getString("name","");
                String age = preferences1.getString("age","");
                //显示
                Toast.makeText(MainActivity.this,"姓名:"+name+" "+"年龄是:"+age,Toast.LENGTH_LONG).show();
                xm.setText("");
                nl.setText("");
            }
        });
    }
}

小组作业 5月9号