请指导我如何在其他活动上调用菜单
这里是主要的活动
package com.droidnova.android.howto.optionmenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class ControlMenu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
Intent intent = new Intent(this, ShowSettings.class);
startActivity(intent);
break;
case R.id.services: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
break;
case R.id.Quit:
new Thread(new Runnable() {
public void run() {
ControlMenu.this.finish();
// The following makes the Android Gods frown upon me
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}).start();
break;
default:
break;
}
return true;
}
}
现在我已经开展了另一项活动,我需要在此
中显示相同的菜单package com.droidnova.android.howto.optionmenu;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class ShowSettings extends Activity {
Prefs myprefs = null;
final String tag = "CH12:ShowSettings";
AlertDialog.Builder adb;// = new AlertDialog.Builder(this);
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.showsettings);
this.myprefs = new Prefs(getApplicationContext());
// load screen
PopulateScreen();
this.adb = new AlertDialog.Builder(this);
final Button savebutton = (Button) findViewById(R.id.settingssave);
// create anonymous click listener to handle the "save"
savebutton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try {
// get the string and do something with it.
final EditText email = (EditText) findViewById(R.id.emailaddress);
if (email.getText().length() == 0) {
AlertDialog ad = ShowSettings.this.adb.create();
ad.setMessage("Please Enter Your Email Address");
ad.show();
return;
}
final EditText serverurl = (EditText) findViewById(R.id.serverurl);
if (serverurl.getText().length() == 0) {
AlertDialog ad = ShowSettings.this.adb.create();
ad.setMessage("Please Enter The Server URL");
ad.show();
return;
}
// save off values
ShowSettings.this.myprefs.setEmail(email.getText().toString());
ShowSettings.this.myprefs.setServer(serverurl.getText().toString());
ShowSettings.this.myprefs.save();
// we're done!
finish();
} catch (Exception e) {
Log.i(ShowSettings.this.tag, "Failed to Save Settings [" + e.getMessage() + "]");
}
}
});
}
private void PopulateScreen() {
try {
final EditText emailfield = (EditText) findViewById(R.id.emailaddress);
final EditText serverurlfield = (EditText) findViewById(R.id.serverurl);
emailfield.setText(this.myprefs.getEmail());
serverurlfield.setText(this.myprefs.getServer());
} catch (Exception e) {
}
}
}
我应该在上面的代码中写什么,这样我的菜单也可以出现在这里。
答案 0 :(得分:3)
将菜单创建为xml资源,然后您可以从任何地方访问它。检查documentation。如果您需要将某个数据仅从一个活动传递到另一个活动,那么扩展应用程序似乎太多了,因为您可以在Intent中将该信息作为额外传递。除此之外,这种做事方式存在“危险”(因为有活动的问题)所以你应该在这种情况下使用SharedPreferences(而不是创建一个Application全局变量)。
答案 1 :(得分:0)
如果您想与您的应用分享一些信息或数据或列表(或任何真实的内容),您应该查看以下链接并按照以下方式制作您的应用: