gpt4 book ai didi

android - 将相同的上下文菜单添加到多个 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:50 25 4
gpt4 key购买 nike

我正在尝试弄清楚如何在多个 Activity 中包含通用代码段。

更具体地说,我有一个上下文菜单,我想将其包含在多个 Activity 中。我看到了这个,但只是不明白如何扩展到多个 Activity 。 http://developer.android.com/guide/topics/ui/menus.html

我将其设置为 Menu.java

    public class Menu extends Activity{

// bottom menus
public static final int Menu1 = 1;
public static final int Menu2 = 2;
public static final int Menu3 = 3;
public static final int Menu4 = 4;
public static final int Menu5 = 5;
public static final int Menu6 = 6;
public static final int Menu7 = 7;


// / Creates the menu items
public boolean onCreateOptionsMenu(Menu menu) {

menu.add(0, Menu3, 0, "Create Profile").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_add));
menu.add(0, Menu5, 0, "Log In").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_login));
menu.add(0, Menu2, 0, "Settings").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_preferences));
menu.add(0, Menu4, 0, "About").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_help));
menu.add(0, Menu1, 0, "Report A Bug").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_start_conversation));
menu.add(0, Menu6, 0, "New Stuff").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_view));
return true;
}



private MenuItem add(int i, int menu32, int j, String string) {
// TODO Auto-generated method stub
return null;
}



// Handles item selections from preference menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu1:
startActivity(new Intent(this, Bug.class));
return true;
case Menu2:
startActivity(new Intent(this, EditPreferences.class));
return true;
case Menu3:
startActivity(new Intent(this, CreateAccount.class));
return true;
case Menu4:
startActivity(new Intent(this, About.class));
return true;
case Menu5:
startActivity(new Intent(this, Login.class));
return true;
case Menu6:
startActivity(new Intent(this, NewAdditions.class));
return true;
}

return false;
}


}

最佳答案

如果您想在超过 1 个 Activity 中添加相同的功能而不是创建 1 个通用 Activity 像 BaseActivity 并扩展该 Activity 将在您继承的所有 Activity 中包含该通用功能

例如我调用了checklogin函数,你可以把你的菜单代码放在这里,

public class BaseActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = getSharedPreferences(PREFS_NAME, 0);
if (IsFullScreen) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

this.CheckLogin();
}

// Check login function
// Your menu code

}

现在你可以在你的 Activity 中扩展它

public class MainScreen extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

this.setContentView(R.layout.mainscreen);

}

}

关于android - 将相同的上下文菜单添加到多个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4894116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com