EditText 正在保存到文件,但未创建文件!
package com.russell.saw;
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 notetaker extends Activity {
public EditText edittext;
public Button save;
public String input;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notetaker);
edittext = (EditText) findViewById(R.id.edittext);
edittext.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
save = (Button)findViewById(R.id.save);
save.setOnClickListener(btnListener);
}
private OnClickListener btnListener = new OnClickListener() {
public void onClick(View v) {
input = edittext.getText().toString();
File file = new File(Environment.getExternalStorageDirectory(), "fileOut.txt");
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
writer.write(input);
writer.newLine();
writer.flush();
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
当我使用此代码单击“保存”时,我没有收到任何文件,任何人都知道我哪里出错了。
(警告:我对 Android 非常陌生,所以如果它明显得令人眼花缭乱,请不要吝啬:3)
package com.russell.saw;
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 notetaker extends Activity {
public EditText edittext;
public Button save;
public String input;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notetaker);
edittext = (EditText) findViewById(R.id.edittext);
edittext.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
save = (Button)findViewById(R.id.save);
save.setOnClickListener(btnListener);
}
private OnClickListener btnListener = new OnClickListener() {
public void onClick(View v) {
input = edittext.getText().toString();
File file = new File(Environment.getExternalStorageDirectory(), "fileOut.txt");
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));
writer.write(input);
writer.newLine();
writer.flush();
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
I'm not getting any file come up when i click save with this code, anyone know where i'm going wrong .
(warning: i am VERY new to android, so please don't be mean if it's blindingly obvious :3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论