小組作業 5月9號
阿新 • • 發佈:2017-05-10
utf-8 parent red oid pan func end edi lns
<?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號