我正在创建一个记录五个 EditText 输入并将它们存储在一个文件中的 android 应用程序。我需要知道的是如何单独检索每个文件并将结果输出到另一个 EditText 字段。
我的示例代码:
package chuck.com;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
public class FillFile extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fill_file);
Button submit = (Button) findViewById(R.id.button1);
submit.setOnClickListener(sub);
Button disp = (Button) findViewById(R.id.button2);
disp.setOnClickListener(dis);
}
private OnClickListener sub = new OnClickListener(){
public void onClick(View v){
try {
EditText one = (EditText) findViewById(R.id.editText1);
EditText two = (EditText) findViewById(R.id.editText2);
EditText three = (EditText) findViewById(R.id.editText3);
EditText four = (EditText) findViewById(R.id.editText4);
EditText five = (EditText) findViewById(R.id.editText5);
FileOutputStream fos;
fos = openFileOutput("XML.txt", MODE_APPEND);
String uno = one.getText().toString();
String dos = two.getText().toString();
String tres = three.getText().toString();
String quatro = four.getText().toString();
String cinco = five.getText().toString();
fos.write(uno.getBytes());
fos.write(dos.getBytes());
fos.write(tres.getBytes());
fos.write(quatro.getBytes());
fos.write(cinco.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private OnClickListener dis = new OnClickListener(){
public void onClick(View v){
Intent transf = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(transf, 0);
}
};
}
我要将文件导出到的 xml 布局:
package chuck.com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File fileDir = getFilesDir();
String fileName = ("XML.txt");
File backup = new File(fileDir, fileName);
boolean content = backup.length() == 0;
try {
backup.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextView one = (TextView) findViewById(R.id.textView1);
TextView two = (TextView) findViewById(R.id.textView2);
TextView three = (TextView) findViewById(R.id.textView3);
TextView four = (TextView) findViewById(R.id.textView4);
TextView five = (TextView) findViewById(R.id.textView5);
EditText eOne = (EditText) findViewById(R.id.editText1);
EditText eTwo = (EditText) findViewById(R.id.editText2);
EditText eThree = (EditText) findViewById(R.id.editText3);
EditText eFour = (EditText) findViewById(R.id.editText4);
Button input = (Button) findViewById(R.id.input_button);
input.setOnClickListener(insert);
if (content)
{
one.setVisibility(View.GONE);
two.setVisibility(View.GONE);
three.setVisibility(View.GONE);
four.setVisibility(View.GONE);
eOne.setVisibility(View.GONE);
eTwo.setVisibility(View.GONE);
eThree.setVisibility(View.GONE);
eFour.setVisibility(View.GONE);
}
else
{
five.setVisibility(View.GONE);
FileInputStream fis;
fis = openFileInput("XML.txt");
eOne.setText(uno);
eTwo.setText(dos);
eThree.setText(tres);
eFour.setText(quatro, cinco);
}
}
private OnClickListener insert = new OnClickListener(){
public void onClick(View v) {
Intent trans = new Intent(v.getContext(), FillFile.class);
startActivityForResult(trans, 0);
}
};
}