Android Studio - 使用菜单中的“保存”按钮将文本写入文件

Android Studio - Writing text to a file using a Save button in the menu

我是 Android 应用程序的初学者,很抱歉浪费您的时间。但是我在这个话题上挣扎了一段时间。我正在构建一个文本编辑器,并且在菜单中有一个保存按钮。我想弄清楚如何使“保存”按钮将文本区域的内容保存到文件中。我的代码中只有一个问题,位于 "public void saveText(View v){":它说无法解析符号 V...

package natanrosenfeld.texteditor;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import com.natanrosenfeld.texteditor.R;
import android.widget.Toast;
import android.view.Gravity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.app.Activity;
import java.io.OutputStreamWriter;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            Intent i = new Intent(getApplicationContext(),SettingsActivity.class);
            startActivity(i);
            Toast.makeText(this, "You opened Settings.",  Toast.LENGTH_SHORT).show();
            break;
        case R.id.action_save: {
            public void saveText(View v){
                try {
                    OutputStreamWriter out = new OutputStreamWriter(openFileOutput("TextFile", MODE_APPEND));
                    EditText ET = (EditText)findViewById(R.id.editText);
                   String text = ET.getText().toString();
                    out.write(text);
                    out.write('\n');
                    out.close();
                    Toast.makeText(this, "The contents are saved in the file.", Toast.LENGTH_LONG).show();
                } catch (Throwable t) {
                    Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
                }
                //Toast.makeText(this, "Save not implemented yet.",Toast.LENGTH_SHORT).show();
                break;
            }
            //noinspection SimplifiableIfStatement
            return super.onOptionsItemSelected(item);
         }
     }
  }
}

试试这个 -MainActivity.class;

public class MainActivity extends ActionBarActivity {

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


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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            Intent i = new Intent(getApplicationContext(),SettingsActivity.class);
            startActivity(i);
            Toast.makeText(this, "You opened Settings.",Toast.LENGTH_SHORT).show();
            break;
        case R.id.action_save:

            savetext();

            break;

    }
    //noinspection SimplifiableIfStatement

    return super.onOptionsItemSelected(item);
}

public void savetext () {
    try {

        OutputStreamWriter out = new OutputStreamWriter(openFileOutput("TextFile", MODE_APPEND));
        EditText ET = (EditText)findViewById(R.id.editText);
        String text = ET.getText().toString();
        out.write(text);
        out.write('\n');
        out.close();
        Toast.makeText(this, "The contents are saved in the file.", Toast.LENGTH_LONG).show();

    } catch (Throwable t) {

        Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();

    }
    //Toast.makeText(this, "Save not implemented yet.", Toast.LENGTH_SHORT).show();
}}

R.layout.activity_main

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<EditText android:text="@string/hello_world" android:layout_width="wrap_content"
    android:id="@+id/editText"
    android:layout_height="wrap_content" /></RelativeLayout>

R.menu.menu_main

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_save" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" /></menu>