Skip to content

Commit

Permalink
Merge pull request #1179 from 1714080902306/master
Browse files Browse the repository at this point in the history
#5 提交实验五代码
  • Loading branch information
zengsn authored Apr 2, 2019
2 parents 3c7f6ef + a58d3ff commit 51f2171
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package edu.hzuapps.androidlabs2019.soft1714080902306;

import android.content.Context;
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.FileInputStream;
import java.io.FileOutputStream;

import edu.hzuapps.androidlabs2019.R;

public class CunChuActivity extends AppCompatActivity {
private EditText et_info01;
private EditText et_info02;
private EditText et_info03;
private EditText et_info04;
private Button btn_save;
private Button btn_read;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cunchu);//获取布局文件中的控件
et_info01=(EditText) findViewById(R.id.et_info01);
et_info02=(EditText) findViewById(R.id.et_info02);
et_info03=(EditText) findViewById(R.id.et_info03);
et_info04=(EditText) findViewById(R.id.et_info04);
btn_save=(Button) findViewById(R.id.btn_save);
btn_read=(Button) findViewById(R.id.btn_read);
btn_save.setOnClickListener(new ButtonListener());
btn_read.setOnClickListener(new ButtonListener());

}
//定义Button按钮的点击事件
private class ButtonListener implements View.OnClickListener {
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn_save:
String hang="\n";
String saveinfo1=et_info01.getText().toString().trim();
String saveinfo2=et_info02.getText().toString().trim();
String saveinfo3=et_info03.getText().toString().trim();
String saveinfo4=et_info04.getText().toString().trim();
FileOutputStream fos;
try {
//保留数据
fos=openFileOutput("data01.txt", Context.MODE_APPEND);
fos.write(hang.getBytes());
fos.write(saveinfo1.getBytes());
fos.write(hang.getBytes());
fos.write(saveinfo2.getBytes());
fos.write(hang.getBytes());
fos.write(saveinfo3.getBytes());
fos.write(hang.getBytes());
fos.write(saveinfo4.getBytes());
fos.write(hang.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(CunChuActivity.this,"数据保存成功",Toast.LENGTH_SHORT).show();
break;
case R.id.btn_read:
String content="";
try {
//获取保存的数据
FileInputStream fis=openFileInput("data01.txt");
byte[] buffer=new byte[fis.available()];
fis.read(buffer);
content=new String(buffer);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(CunChuActivity.this,"保存的数据是:"+content,Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
android:id="@+id/textView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="25dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="80dp"
android:text="请输入出发地:" />

<EditText
android:id="@+id/et_info01"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignLeft="@id/textView01"
android:layout_alignBottom="@+id/textView01"
android:layout_marginLeft="186dp"
android:layout_marginBottom="-6dp" />

<TextView
android:id="@+id/textView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="25dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="130dp"
android:text="请输入目的地:" />

<EditText
android:id="@+id/et_info02"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignLeft="@id/textView02"
android:layout_alignBottom="@+id/textView02"
android:layout_marginLeft="186dp"
android:layout_marginBottom="-6dp" />

<TextView
android:id="@+id/textView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="25dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="180dp"
android:text="请输入乘客人数:" />

<EditText
android:id="@+id/et_info03"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignLeft="@id/textView03"
android:layout_alignBottom="@+id/textView03"
android:layout_marginLeft="186dp"
android:layout_marginBottom="-6dp" />

<TextView
android:id="@+id/textView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="25dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="230dp"
android:text="请输入出发时间:" />

<EditText
android:id="@+id/et_info04"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignLeft="@id/textView04"
android:layout_alignBottom="@+id/textView04"
android:layout_marginLeft="186dp"
android:layout_marginBottom="-6dp" />

<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginBottom="198dp"
android:text="保存信息"
android:textSize="25dp" />

<Button
android:id="@+id/btn_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="60dp"
android:layout_marginRight="60dp"
android:layout_marginBottom="195dp"
android:text="读取信息"
android:textSize="25dp" />

</RelativeLayout>

0 comments on commit 51f2171

Please sign in to comment.