【问题标题】:Save EditText to file with Button - Android, Java使用 Button 将 EditText 保存到文件 - Android、Java
【发布时间】:2013-11-10 14:56:11
【问题描述】:

我有一个 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}

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

任何帮助都会很棒。谢谢

【问题讨论】:

    标签: java android file save android-edittext


    【解决方案1】:

    ETName 可能是 EditText。您没有发布太多代码,所以它应该是这样的: ETName.getText().toString();View v 中更改View ETName(它是单击一个不在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(); }
    
    
                        }
    

    【讨论】:

    • 抱歉,ETName 是我要从中保存内容的 EditText
    • 然后尝试:ETName.getText().toString();我希望它会帮助你。如果您觉得有帮助,请将答案标记为有用。
    • 我已经编辑了问题中的代码。但是,当我按下按钮记录文本时,应用程序崩溃了。 LogCat有很多错误:
    • 在此处复制您的 logcat。
    • 你在那个路径中有这个文件吗:"/sdcard/Accelerometer.html"?
    【解决方案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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多