我需要一个简单的应用程序在手机(根或SD卡)中制作文本文件(.txt),其中包含来自更多edittext的各种字符串。按钮确认输入数据。
文件中输出的示例必须是:
output.txt的
John ; Smith ; 10/05/1970 ;
我也有兴趣将数据导出到CSV文件。这是代码:
MainActivity.java
package com.example.utente.questionario;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText) findViewById(R.id.editText1);
final EditText editText2 = (EditText) findViewById(R.id.editText2);
final EditText editText3 = (EditText) findViewById(R.id.editText3);
final Button button1 = (Button) findViewById(R.id.button1);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.utente.questionario.MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name"
android:layout_marginTop="74dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Surname"
android:layout_below="@+id/editText1"
android:layout_alignStart="@+id/editText1"
android:layout_marginTop="42dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birth date"
android:textSize="18dp"
android:layout_marginBottom="67dp"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate file"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date"
android:hint="dd/mm/yyyy"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/editText2" />
</RelativeLayout>
在Riten的回答之后更新: MainActivity.java :
package com.example.utente.questionario;
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.File;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText) findViewById(R.id.editText1);
final EditText editText2 = (EditText) findViewById(R.id.editText2);
final EditText editText3 = (EditText) findViewById(R.id.editText3);
final Button button1 = (Button) findViewById(R.id.button1);
String name = editText1.getText().toString();
String surname = editText2.getText().toString();
String birthDate = editText3.getText().toString();
private String getOrderText(String aa, String bb, String cc){
StringBuilder sb = new StringBuilder();
sb.append(aa+ ";");
sb.append(" " + bb+ ";");
sb.append(" " + cc);
return sb.toString();
}
try {
FileOutputStream fileOut=openFileOutput("FileName.txt", MODE_APPEND); // you can directly give file name as FileName.csv
OutputStreamWriter outputWriter=new OutputStreamWriter(fileOut);
outputWriter.write(getOrderText(name, surname, birthDate));
outputWriter.write("\n");
outputWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
你可以试试这个:
try {
FileOutputStream fileOut=openFileOutput("FileName.txt", MODE_APPEND); // you can directly give file name as FileName.csv
OutputStreamWriter outputWriter=new OutputStreamWriter(fileOut);
outputWriter.write(getOrderText("string from edittext", "string from edittext", "string from edittext"));
outputWriter.write("\n");
outputWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
然后 getOrderText 可以定义为:
private String getOrderText(String aa, String bb, String cc){
StringBuilder sb = new StringBuilder();
sb.append(aa+ ";");
sb.append(" " + bb+ ";");
sb.append(" " + cc);
return sb.toString();
}
希望这可以帮助你...