使用SharePreferences将姓名和年龄信息保存到文件中,并读取
首先这个界面与开学不久学的登录界面差不多,这个对我们已经很容易完成
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/ev_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/ev_userName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/ev_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/ev_password" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="187dp"
android:layout_height="wrap_content"
android:onClick="onClick"
android:id="@+id/writer"
android:text="@string/writer"/>
<Button
android:layout_width="204dp"
android:layout_height="wrap_content"
android:onClick="onClick"
android:id="@+id/read"
android:text="@string/read"/>
</LinearLayout>
效果为:
使用Java程序完成写入和读取,接下来代码中的细节我将会用注释在代码中标出来
public class ShareActivity 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_share);
//获取这些组件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( ShareActivity.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(ShareActivity.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;
}
}
}
效果为:
这次作业相对来说还比较简单,因为这仅涉及一个界面,而且我们对登录界面的设计又比较熟悉,知道一些固定的步骤,完成还是比较轻松的