SharedPreferences

这次任务主要就是通过获取数据保存后来读取数据,在界面完成上通过代码实现功能,总体来说较容易,按照步骤完成定义获取组件,以及实现存储数据文件并读取后显示

主界面布局如下


    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/name" />
    <EditText
        android:id="@+id/age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/age"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

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

        <Button
            android:id="@+id/in"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/in"
            android:textSize="20sp" />

        <Button
            android:id="@+id/read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/read"
            android:textSize="20sp"/>
    </LinearLayout>



实现功能如下

package com.example.bwqpzy.sharepreferences;

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 evname;
        private EditText evage;
        //确定按钮
        private Button in;
        private Button read;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //获取组件
          evname = (EditText) findViewById(R.id.name);
           evage = (EditText) findViewById(R.id.age);
           in= (Button)findViewById(R.id.in);
            //设置按钮监听事件
           in.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String name = evname.getText().toString();
                    String age =evage.getText().toString();

                    if(saveToFile(name)){
                        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 = evname.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 = evname.getText().toString();
                    String age =evage.getText().toString();
                    Toast.makeText(MainActivity.this, "输入姓名"+username+",年龄:" + age,Toast.LENGTH_SHORT).show();
                }
            });
        }

        private boolean saveToFile(String userName) {
            try {
                FileOutputStream out = openFileOutput("data.txt",  MODE_PRIVATE );
                BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( out ) );
                writer.write(userName);
                out.close();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

    }



[写入]

[读取]

>>> 请问怎么学android? - 小白 >> 自己问老师! - 愤青 > 老师在哪? - 小白
posted @ 2017-05-08 16:18  berice  阅读(130)  评论(0编辑  收藏  举报