1

我有一个 EditText,用户在其中输入他们的名字,然后我想按下一个按钮,并将 EditText 的内容传输到一个文件中。

编辑:到目前为止我有这个代码:

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}

但这只是了解印刷作品的占位符。我想看看用户在那里打印的内容。

任何帮助都会很棒。谢谢

4

2 回答 2

2

ETName 可能是 EditText。您没有发布太多代码,所以它应该是: ETName.getText().toString();更改View ETNameView 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(); }


                    }
于 2013-11-10T15:09:11.967 回答
2
 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();
    }
于 2014-10-03T09:08:05.227 回答