0
我需要一個簡單的應用程序,在手機(根或SD卡)中使用來自更多edittext的各種字符串來創建文本文件(.txt)。按鈕確認輸入數據。使用edittext中的字符串保存文本或CSV文件
文件輸出的例子必須是:
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>
更新:MainActivity.java Riten的回答後:
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();
}
有沒有在一些錯誤GetOrderText方法? 「私人字符串getOrderText」之後的Android Studio建議「預期」;並在「返回sb.toString();」之後建議「無法從具有無效結果類型的方法返回值」。 – Miche85
@ Miche85不,沒有這樣的錯誤。它工作得很好..請檢查你的返回類型getOrderText方法... – Riten
你能告訴我更正的代碼與try-catch和getOrderText聯合在一起只有一個文本?謝謝 – Miche85