代码之家  ›  专栏  ›  技术社区  ›  Natan Rosenfeld

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

  •  -1
  • Natan Rosenfeld  · 技术社区  · 11 年前

    我是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);
             }
         }
      }
    }
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   ZaLeX    11 年前

    尝试此-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、 布局活动时间

    <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_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>