获取EditText上内容分行写入文件并读取

一、完成基本结构布局

获取EditText上内容分行写入文件并读取_第1张图片

二、根据id找到每个Button和EditText并设置按钮点击事件监听

获取EditText上内容分行写入文件并读取_第2张图片

获取EditText上内容分行写入文件并读取_第3张图片

三、使用editText1.getText().toString()语句获取到EditText内容并通过点击事件实现转换和运算

获取EditText上内容分行写入文件并读取_第4张图片

 四、写一个文件辅助类,为后续将内容写入文件做准备

获取EditText上内容分行写入文件并读取_第5张图片

 这里文件内容已基本写完,只需再完善主函数即可。

获取EditText上内容分行写入文件并读取_第6张图片

到这里已经是可以将EditText上内容写到文件里了,最后在利用点击事件读取出文件内容

获取EditText上内容分行写入文件并读取_第7张图片

如果想要获取多行EditText上内容并不覆盖写入文件里只需将辅助类FileHelper中save函数参数个数改变再换行分别写入即可

获取EditText上内容分行写入文件并读取_第8张图片

 获取EditText上内容分行写入文件并读取_第9张图片

最后结果如下:

获取EditText上内容分行写入文件并读取_第10张图片 

 源码如下:

1.activity_main.xml




    

    

    

    

    

    

    

    

    

        

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;
        }
    }
}

 

 

 

 

 

 

 
  

 

 

 

 

 

 

 

你可能感兴趣的:(android,studio,java,android)