Вопрос:
У меня есть EditText, в котором пользователь вводит свое имя, затем я хочу нажать кнопку, и содержимое EditText будет перенесено в файл.
EDITED: У меня есть этот код:
Button submitButton = (Button) findViewById(R.id.baddNametoFile); submitButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try{ ETName.getText().toString(); PrintWriter writer = new PrintWriter(new BufferedWriter( new FileWriter(«/sdcard/Accelerometer.html», true))); writer.println(«<h3 style=padding-left:20px;>» + ETName + «</h3><br>»); // new arraylist writer.close(); }catch (IOException e) { // put notification here later!!! e.printStackTrace(); } } });
У меня есть печать в файле, каково фактическое значение Android:
android.widget.Button{……./baddNametoFile}
Но это просто место, чтобы знать печатные работы. Я хотел бы видеть, что пользователь напечатал там там.
Любая помощь будет большой. благодаря
Лучший ответ:
ETName – это, вероятно, EditText. Вы не опубликовали много кода, чтобы это было похоже: ETName.getText().toString(); Изменить View ETName в View v (нажмите кнопку не на EditText). Давайте попробуем вот так:
@Override public void onClick(View v) { // TODO Auto-generated method stub try{ String etName = ETName.getText().toString(); if(!etName.trim().equals(«»)){ File file =new File(«/sdcard/Accelerometer.html»); //if file doesnt exists, then create it if(!file.exists()){ file.createNewFile(); } FileWriter fileWritter = new FileWriter(file.getName(),true); BufferedWriter bufferWritter = new BufferedWriter(fileWritter); bufferWritter.write(etName); bufferWritter.close(); } }catch (IOException e) { e.printStackTrace(); } } Ответ №1 try{ File dir = new File(«/mnt/sdcard/MyApp/»); if (!dir.exists()) { dir.mkdir(); System.out.println(«Directory created»); } //ceate .rtf file with header myFile = new File(«/mnt/sdcard/MyApp/Student.rtf»); if (!myFile.exists()) { myFile.createNewFile(); System.out.println(«File created»); } fOut = new FileOutputStream(myFile.getAbsoluteFile(),true); myOutWriter = new OutputStreamWriter(fOut); //Write the header : in your case it is : Student class Marsk (only one time) myOutWriter.write(«Student,class,Marks»+»n»); myOutWriter.flush(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); }