尝试此-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>