gpt4 book ai didi

java - 在外部类中使用微调器,因为所有其他 Activity 都需要

转载 作者:行者123 更新时间:2023-12-01 12:35:40 27 4
gpt4 key购买 nike

我的问题是如何将我的微调器放入外部 java 类中并在所有其他 Activity 中实现它(作为菜单),这是我的微调器代码:

final Spinner spinner = (Spinner) findViewById(R.id.comboCasino);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(0, false);
// this will be called when you select any item in this spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
// get the text at that position
switch(position) {
case 0: {
Intent NewPost = new Intent(getApplicationContext(), StartingPoint.class);
startActivity(NewPost);
break; }
case 1: {
Intent NewPost = new Intent(getApplicationContext(), Simmering.class);
startActivity(NewPost);
break; }
case 2: {
Intent NewPost = new Intent(getApplicationContext(), LugnerCity.class);
startActivity(NewPost);
break; }
case 3: {
Intent NewPost = new Intent(getApplicationContext(), Gmunden.class);
startActivity(NewPost);
break;}
case 4: {
Intent NewPost = new Intent(getApplicationContext(), Salzburg.class);
startActivity(NewPost);
break; }
case 5: {
Intent NewPost = new Intent(getApplicationContext(), Linz.class);
startActivity(NewPost);
break; }
case 6: {
Intent NewPost = new Intent(getApplicationContext(), Saalbach.class);
startActivity(NewPost);
break; }
case 7: {
Intent NewPost = new Intent(getApplicationContext(), Innsbruck.class);
startActivity(NewPost);
break;}
case 8: {
Intent NewPost = new Intent(getApplicationContext(), Reutte.class);
startActivity(NewPost);
break; }
case 9: {
Intent NewPost = new Intent(getApplicationContext(), Bregenz.class);
startActivity(NewPost);
break; }
case 10: {
Intent NewPost = new Intent(getApplicationContext(), Kufstein.class);
startActivity(NewPost);
break; }
case 11: {
Intent NewPost = new Intent(getApplicationContext(), Bratislava.class);
startActivity(NewPost);
break; }
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

它应该保存在例如menu.java中,并且应该在每个 Activity 中调用,我如何正确地做到这一点?预先感谢您。

最佳答案

如果您的所有 Activity 都有相同的菜单,则最好的方法是创建一个扩展 Activity 的父类(super class),并让所有其他 Activity 扩展此 Activity 。

public class BaseActivity extends Activity { // menu code  }

public class StartingPoint extends BaseActivity { //... }

关于java - 在外部类中使用微调器,因为所有其他 Activity 都需要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25623685/

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