0

私は現在Androidアプリにこのページを持っています:

これらのフィールドに追加されたデータを、デバイスに保存されている.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 () ; 
  } 
}

これで、編集テキストフィールドがある場所に変数が渡されると確信していますか?これが機能するように変数をリンクするにはどうすればよいですか?プロジェクトのコードが必要な場合は、アップロードすることができます。これにより、コードが簡単になります。

4

1 に答える 1

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()し、適切なパラメーターを使用して読み取り/書き込みメソッドを呼び出すことができます。

于 2012-07-27T21:24:50.947 に答える