在Android中传递变量

时间:2012-07-27 20:03:58

标签: java android eclipse variables

我目前在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 () ; 
  } 
}

现在我确定变量是在我编辑文本字段的地方传递的?如何链接变量以便这可以工作?如果需要我的项目代码,我可以上传它,如果这样可以更容易。

1 个答案:

答案 0 :(得分: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()方法中创建该类的实例,并使用适当的参数在其上调用读/写方法。 / p>