使用sharedpreferences将姓名和年龄信息保存到文件中

**本次作业和刚开始要求做的登陆界面大同小异,比较容易实现

**XML布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mpyypm.sharedpreferences.MainActivity">

<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">

<EditText
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:hint="@string/ev_userName"
    android:id="@+id/ev_userName"/>
</LinearLayout>


<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">

<EditText
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:hint="@string/ev_password"
    android:id="@+id/ev_age"/>

</LinearLayout>


-<LinearLayout
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="wrap_content"
android:layout_weight="1">

<Button
    android:layout_height="wrap_content"
    android:layout_width="187dp"
    android:id="@+id/writer"
    android:text="@string/writer"
    android:onClick="onClick"/>

<Button
    android:layout_height="wrap_content"
    android:layout_width="204dp"
    android:id="@+id/read"
    android:text="@string/read"
    android:onClick="onClick"/>

</LinearLayout>

</LinearLayout>

1130624-20170508225449285-633647544.png

**JAVA代码的实现



package com.example.mpyypm.sharedpreferences;

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;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {
//定义相关组件
private EditText evuserName;
private EditText evage;
//确定按钮
private Button writer;
private Button read;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获取这些组件findViewId(让其发生关联)
    evuserName = (EditText) findViewById(R.id.ev_userName);
    evage = (EditText) findViewById(R.id.ev_age);
    writer= (Button)findViewById(R.id.writer);
    //设置按钮(写入)监听事件
    writer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String userName = evuserName.getText().toString();
            String age = evage.getText().toString();
            //根据情况进行存储,这里需要在OnClick方法外使用一个saveToFile方法,对其进行写入
            if(saveToFile(userName)){
                Toast.makeText( MainActivity.this,"保存成功" ,Toast.LENGTH_SHORT).show();

            }
        }
    });
    //设置按钮(读取)监听事件
    read = (Button)findViewById(R.id.read);
    read.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String userName = evuserName.getText().toString();
            String filename = "data.txt";
            String result = "";
            try{
                //打开指定文件,并读取其数据
                FileInputStream in = openFileInput( filename );
                BufferedReader reader = new BufferedReader( new InputStreamReader( in ) );
                userName = reader.readLine();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            userName = evuserName.getText().toString();
            String age = evage.getText().toString();
            Toast.makeText(MainActivity.this, "输入姓名"+userName+",年龄:" + age,Toast.LENGTH_SHORT).show();
        }
    });
}

private boolean saveToFile(String userName) {
    //1.打开文件
    try {
        FileOutputStream out = openFileOutput("data.txt",  MODE_PRIVATE );
        //2.写入
        BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out ) );
        writer.write(userName);
        //3.关闭文件输入流
        out.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

}

1130624-20170508225508410-156951510.png
1130624-20170508225523988-1916771465.png

转载于:https://www.cnblogs.com/mpy1611/p/6828388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值