将多个字符串写入文件并添加新行

时间:2014-01-13 13:24:51

标签: android eclipse

我正在构建一个您从微调器中选择的基本应用程序,选择一种颜色并单击“保存”。如果您要查找的颜色不在微调器中,则按一个按钮,您可以将其键入EditText。 我正在尝试这样做,所以我可以写出所选择的微调器/ edittext颜色和“colors.txt”文件中的日期。目前,没有错误,但是当我在我的Android上启动应用程序时,它会立即崩溃。

Colorchooser.java

package com.example.scalendar;

import java.io.IOException;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;

public class Colorchooser extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_colorchooser);
    }


    public void writetoFile(String str) {
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("colors.txt", Context.MODE_PRIVATE));
            outputStreamWriter.write("colorstring , datestring");
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        } 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.colorchooser, menu);
        return true;
    }  
}`

这是我的Strings.XML文件

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">SCalendar</string>
    <string name="action_settings">Settings</string>
    <string name="color_prompt">What was todays color?</string>
    <string name="SaveButtonText">Save To Calendar</string>
    <string name="textfieldhint">Enter color</string>
    <string name="otherbuttontext">Color not listed</string>
    <string name="othercolorexplanationtext">Dont see the color here? Click the button below and type it yourself.</string>


    <string-array name="color_arrays">
        <item>Select a color:</item>
        <item>Purple</item>
        <item>Orange</item>
        <item>Green</item>
        <item>Red</item>
        <item>Blue</item>
        <item>Pink</item>
        <item>Brown</item>
        <item>Yellow</item>
        <item>Black</item>
        <item>White</item>
    </string-array>

            <string name="colorstring">
                colorSpinner = findViewById(R.id.colorSpinner)
                colorSpinner.getSelectedItem().toString()
            </string>

            <string name="datestring"> 
            SimpleDateFormat formatter = new SimpleDateFormat("dd_MM_yyyy");
            Date now = new Date();
            String fileName = formatter.format(now).toString();
            </string>


</resources>`

这是我的Mainactivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="Colorchooser" >

<Spinner
    android:id="@+id/colorSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:entries="@array/color_arrays"
    android:prompt="@string/color_prompt" />

<TextView
    android:id="@+id/othercolorexplaination"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/colorSpinner"
    android:layout_below="@+id/colorSpinner"
    android:layout_marginTop="15dp"
    android:text="@string/othercolorexplanationtext"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/ButtonSave"
    style="@style/AppTheme"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/othercolorexplaination"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="62dp"
    android:onClick="writetoFile(String getcolor)"
    android:text="@string/SaveButtonText" />

<EditText
    android:id="@+id/othercolor"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/ButtonSave"
    android:layout_below="@+id/otherbutton"
    android:layout_marginTop="42dp"
    android:ems="10"
    android:focusable="false"
    android:hint="@string/textfieldhint"
    android:inputType="textEmailAddress" >
</EditText>

<Button
    android:id="@+id/otherbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/othercolorexplaination"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:onClick="othercolor.focusableInTouchMode(true)"
    android:text="@string/otherbuttontext" />
</RelativeLayout>`

1 个答案:

答案 0 :(得分:1)

对于Java字符串与字符串变量之间的关系,以及字符串变量与字符串资源之间的关系,您似乎存在一些重大误解:

  1. 此:

     outputStreamWriter.write("colorstring , datestring");
    

    将始终将“colorstring,datestring”写入您的文件,因此该文件将如下所示:

    colorstring, datestring
    colorstring, datestring
    colorstring, datestring
    
  2. 您不能指望在字符串资源中执行Java代码:

    <string name="colorstring">
            colorSpinner = findViewById(R.id.colorSpinner)
            colorSpinner.getSelectedItem().toString()
    </string>
    

    资源中的字符串为Constants。在运行时无法更改它们。