2012-07-27 45 views
0

我目前在我的Android應用程序此頁:傳遞一個變量的Android

http://i50.tinypic.com/iwhci1.png

我想通過加入這些字段存儲在設備上的.txt文件中的數據。下面

代碼:

<Button 
android:id="@+id/Button02" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:text="@string/btn2" /> 
<ScrollView android:layout_width="match_parent" android:id="@+id/scrollView1" android:layout_height="wrap_content"> 
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> 
<TextView android:text="Please insert details below to confirm you have completed and understood the app" android:id="@+id/textView1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_gravity="center_horizontal"> 
</TextView> 
</LinearLayout> 
</ScrollView> 
     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:text="Forename" > 

      <requestFocus /> 
     </EditText> 

     <EditText 
      android:id="@+id/editText2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:text="Surname" /> 

     <EditText 
      android:id="@+id/editText3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="number" 
      android:text="Staff ID" /> 

     <Button 
      android:id="@+id/buttonformdata" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Submit" /> 
</LinearLayout> 

我相信這就是所謂的FileWriter?我在我的項目中創建了一個名爲'FileWriterExample.java'的.java文件。我正在努力的是能夠在我的XML文件'Screen10.xml'和這個'FileWriterExample'java文件之間傳遞數據。

在我FileWriterExample文件我有以下代碼:

package org.example.screens; 

import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 

public class FileWriterExample { 

    FileWriter writer; 
    File file; 

    public void write() { 
    // Create File 
    file = new File ("FileWriterTest.txt") ; 
    try { 
     // new FileWriter (file, true) - if the file already exists 
     // the bytes are written to the end of the file 

     // new FileWriter (file) - if the file already exists 
     // this will overwrite 
     writer = new FileWriter (file, true) ; 

     // text is written to the stream 
    writer.write (edittext1 +" ") ; 

     // text is written to the stream 
    writer.write (edittext2 +" ") ; 

     // text is written to the stream 
    writer.write (edittext3 +" ") ; 

     // Write the stream to the file 
     // Should always be run at the end, so that the stream 
     // is empty and everything in the file . stands 
     writer.flush() ; 

     // Closes the stream 
     writer.close() ; 
    } catch (Exception e) { 
     e.printStackTrace() ; 
    } 
    } 

    public static void main (String [] args) { 
    FileWriterExample fileWriterExample = new FileWriterExample() ; 
    fileWriterExample.write() ; 
    } 
} 

現在,我敢肯定,變量傳遞在那裏我有編輯文本字段?我如何鏈接變量,以便它可以工作?如果需要我的項目代碼,我可以上傳它,如果這樣會更容易。

回答

0

我假設你想在用戶點擊提交按鈕時將數據寫入文件。你可以這樣做聽衆的onClick()方法附加到這個按鈕裏面如下:

try{ 

FileOutputStream fout = openFileOutput(「yourfile.txt」,MODE_PRIVATE); 

OutputStreamWriter osw = new OutputStreamWriter(fOut); 

osw.write(editText1.getText().toString()+" "); 
osw.write(editText2.getText().toString()+" "); 
osw.write(editText3.getText().toString()+" "); 

osw.close(); 

fout.close(); 

}catch(Exception e){ 

//do the exception handling 
} 

如果你打算使用自己的類來處理所有的寫作和閱讀,您可以創建的一個實例在onClick()方法中使用類,並使用適當的參數在其上調用讀/寫方法。

+0

所以這將工作? http://pastebin.com/9GS4wffq – user1558614 2012-07-27 22:12:28