有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Android中传递变量

我的Android应用程序中当前有此页面:

我想将添加到这些字段中的数据传递给一个。存储在设备上的txt文件

代码如下:

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

            <requestFocus />
        </EditText>

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

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

        <Button
            安卓:id="@+id/buttonformdata"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓: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) 个答案

  1. # 1 楼答案

    我假设您希望在用户单击submit按钮时将数据写入文件。您可以在附加到此按钮的侦听器的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()方法内创建该类的实例,并使用适当的参数对其调用读/写方法