一、完成基本结构布局
二、根据id找到每个Button和EditText并设置按钮点击事件监听
三、使用editText1.getText().toString()语句获取到EditText内容并通过点击事件实现转换和运算
?四、写一个文件辅助类,为后续将内容写入文件做准备
?这里文件内容已基本写完,只需再完善主函数即可。
到这里已经是可以将EditText上内容写到文件里了,最后在利用点击事件读取出文件内容
如果想要获取多行EditText上内容并不覆盖写入文件里只需将辅助类FileHelper中save函数参数个数改变再换行分别写入即可
?
最后结果如下:
?
?源码如下:
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入文件名" />
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="油价(元/升)" />
<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="需要加入的油量(升)" />
<EditText
android:id="@+id/edittext2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="最大里程(公里)" />
<EditText
android:id="@+id/edittext3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="205dp"
android:layout_height="wrap_content"
android:text="写入" />
<Button
android:id="@+id/button1"
android:layout_width="205dp"
android:layout_height="wrap_content"
android:text="清空" />
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示文件中内容" />
</LinearLayout>
2.FileHelper.java
package com.example.work4;
import android.content.Context;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileHelper {
private Context mContext;
public FileHelper() {
}
public FileHelper(Context mContext) {
super();
this.mContext = mContext;
}
public void save(String adNum, String time1,String time2,String time3,String time4) {
String content = "油价" + time1 + "元" ;
String content1 = "需要加入的油量:" + time2 + "升" ;
String content2 = "最大里程:" + time3 + "公里" ;
String content3 = "平均油耗:" + time4 + "升/公里" ;
FileOutputStream fos = null;
try {
fos = mContext.openFileOutput(adNum, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.write("\r\n".getBytes());
fos.write(content1.getBytes());
fos.write("\r\n".getBytes());
fos.write(content2.getBytes());
fos.write("\r\n".getBytes());
fos.write(content3.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public String read(String filename) throws IOException {
FileInputStream input = mContext.openFileInput(filename);
byte[] temp = new byte[1024];
StringBuilder sb = new StringBuilder("");
int len = 0;
while ((len = input.read(temp)) > 0) {
sb.append(new String(temp, 0, len));
}
input.close();
return sb.toString();
}
}
?3.MainActivity.java
package com.example.work4;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private EditText editText1;
private EditText editText2;
private EditText editText3;
private Button button;
private Button button1;
private Button button2;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
bindViews();
}
private void bindViews() {
editText = (EditText) findViewById(R.id.edittext);
editText1 = (EditText) findViewById(R.id.edittext1);
editText2 = (EditText) findViewById(R.id.edittext2);
editText3 = (EditText) findViewById(R.id.edittext3);
button = (Button) findViewById(R.id.button);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(this::onClick);
button1.setOnClickListener(this::onClick);
button2.setOnClickListener(this::onClick);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
editText.setText("");
editText1.setText("");
editText2.setText("");
editText3.setText("");
break;
case R.id.button:
FileHelper fHelper = new FileHelper(mContext);
String filename = editText.getText().toString();
String t1 = editText1.getText().toString();
String t2 = editText2.getText().toString();
String t3 = editText3.getText().toString();
int c1=Integer.parseInt(editText1.getText().toString());
int c2=Integer.parseInt(editText2.getText().toString());
int c3=Integer.parseInt(editText3.getText().toString());
double c4=(double)c2/c3;
String fromDouble=""+ c4;
try {
fHelper.save(filename, t1,t2,t3,fromDouble);
Toast.makeText(getApplicationContext(), "数据写入成功",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "数据写入失败",
Toast.LENGTH_SHORT).show();
}
break;
case R.id.button2:
String text1="";
FileHelper fHelper2 = new FileHelper(getApplicationContext());
try {
String Text = editText.getText().toString();
text1 = fHelper2.read(Text);
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), text1, Toast.LENGTH_SHORT).show();
break;
}
}
}
?
?
?
?
?
?
?
?
?
?
?
?
?
|