代码改变世界

使用SharedPreferences将姓名和年龄信息保存到文件,然后再读取

2017-05-09 20:25  好名字啊  阅读(494)  评论(0)    收藏  举报
main xml
<EditText
    android:id="@+id/ev_userName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入姓名:" />
<EditText
    android:id="@+id/ev_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入年龄:" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
<Button
    android:id="@+id/btn_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="写入"
    android:textSize="20sp"
    android:onClick="onClick" />
<Button
    android:id="@+id/btn_login2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="读取"
    android:textSize="20sp"
    android:onClick="onClick" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:id="@+id/tv_text"
        android:text=""/>
</LinearLayout>
java
定义
private EditText ev_userName;
private EditText password;
private Button write;
private Button read;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ev_userName = (EditText) findViewById(R.id.ev_userName);
    password = (EditText) findViewById(R.id.ev_password);
    write = (Button) findViewById(R.id.btn_login);
    read = (Button) findViewById(R.id.btn_login2);
写入
write.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String Name = ev_userName.getText().toString();
            String age = password.getText().toString();
            if (saveToFile(Name)) {
                Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();

            }
        }
    });
读取
read.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String filename = "data.txt";
            String result = "";
            String uesrName;
            try {
                FileInputStream in = openFileInput(filename);
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                uesrName= reader.readLine();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
            uesrName = ev_userName.getText().toString();
            String age = password.getText().toString();
            Toast.makeText(MainActivity.this, "输入姓名" + uesrName + ",年龄:" + 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;
    }
}

}