我想從打印到文件的EditText文本。因此,當用戶輸入消息並點擊提交按鈕時,消息被寫入文件。
這裏是我的代碼:寫入文本文件 - 安卓
package com.getText.android;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
public class getText extends Activity {
public EditText ET;
public Button button1;
public String input;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ET = (EditText) findViewById(R.id.edittext);
ET.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
button1 = (Button)findViewById(R.id.btnClickMe);
button1.setOnClickListener(btnListener);
}
private OnClickListener btnListener = new OnClickListener(){
public void onClick(View v){
input = ET.getText().toString();
try{
File file = new File(Environment.getExternalStorageDirectory(), "fileOut.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
writer.write(input);
writer.newLine();
writer.flush();
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
}
應用程序運行得很好,但文件還沒有生成!有人能告訴我什麼是錯的嗎? :)