gpt4 book ai didi

android - 从其他 Activity 调用菜单

转载 作者:行者123 更新时间:2023-11-29 18:20:27 27 4
gpt4 key购买 nike

请指导我如何调用其他 Activity 的菜单

这里是主要 Activity

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;
}
}

现在我进行了另一项 Activity ,我需要在此 Activity 中显示相同的菜单

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) {

}
}
}

我应该在上面的代码中写些什么,这样我的菜单也可以出现在这里。

最佳答案

将菜单创建为 xml 资源,然后您就可以从任何地方访问它。检查documentation .如果您只需要将某些数据从一个 Activity 传递到另一个 Activity ,那么扩展 Application 似乎太多了,因为您可以在 Intent 中将信息作为 en extra 传递。此外,这种做事方式存在“危险”(由于 activity lyfecicle),因此在这种情况下您应该使用 SharedPreferences(而不是创建 Application 全局变量)。

关于android - 从其他 Activity 调用菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5819773/

27 4 0