Java 在外部类中使用微调器,因为所有其他活动都需要微调器
我的问题是如何将微调器放在外部java类中,并在所有其他活动(用作菜单)中实现它,以下是我的微调器代码:Java 在外部类中使用微调器,因为所有其他活动都需要微调器,java,android,class,spinner,external,Java,Android,Class,Spinner,External,我的问题是如何将微调器放在外部java类中,并在所有其他活动(用作菜单)中实现它,以下是我的微调器代码: final Spinner spinner = (Spinner) findViewById(R.id.comboCasino); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simp
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
}
});
最终微调器微调器=(微调器)findViewById(R.id.comboCasino);
ArrayAdapter=ArrayAdapter.createFromResource(这个,R.array.comboCasino,android.R.layout.simple\u微调器\u项);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
旋转器。设置适配器(适配器);
微调器选择(0,false);
//选择此微调器中的任何项目时,将调用此微调器
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共位置(AdapterView父视图、视图arg1、整型位置、长arg3){
//在该位置获取文本
开关(位置){
案例0:{
Intent NewPost=newintent(getApplicationContext(),StartingPoint.class);
startActivity(NewPost);
中断;}
案例1:{
Intent NewPost=newintent(getApplicationContext(),Simmering.class);
startActivity(NewPost);
中断;}
案例2:{
Intent NewPost=newintent(getApplicationContext(),LugnerCity.class);
startActivity(NewPost);
中断;}
案例3:{
Intent NewPost=newintent(getApplicationContext(),Gmunden.class);
startActivity(NewPost);
中断;}
案例4:{
Intent NewPost=newintent(getApplicationContext(),Salzburg.class);
startActivity(NewPost);
中断;}
案例5:{
Intent NewPost=newintent(getApplicationContext(),Linz.class);
startActivity(NewPost);
中断;}
案例6:{
Intent NewPost=newintent(getApplicationContext(),Saalbach.class);
startActivity(NewPost);
中断;}
案例7:{
Intent NewPost=newintent(getApplicationContext(),Innsbruck.class);
startActivity(NewPost);
中断;}
案例8:{
Intent NewPost=newintent(getApplicationContext(),Reutte.class);
startActivity(NewPost);
中断;}
案例9:{
Intent NewPost=newintent(getApplicationContext(),Bregenz.class);
startActivity(NewPost);
中断;}
案例10:{
Intent NewPost=newintent(getApplicationContext(),Kufstein.class);
startActivity(NewPost);
中断;}
案例11:{
Intent NewPost=newintent(getApplicationContext(),Bratislava.class);
startActivity(NewPost);
中断;}
}
}
@凌驾
未选择公共无效(AdapterView父级){
//TODO自动生成的方法存根
}
});
例如,它应该保存在menu.java中,并且应该在每个活动中调用它,我如何正确地做到这一点?提前感谢。如果您的所有活动都有相同的菜单,那么最好的方法是创建一个扩展活动的超类,并让您的所有其他活动扩展此活动
public class BaseActivity extends Activity { // menu code }
public class StartingPoint extends BaseActivity { //... }
menu(Context mcontext){
this.mcontext = mcontext;
}
Menu object = new Menu(object_of_current_class);
object.ShowSpinner();
尝试创建自己的微调器类:
public class MenuSpinner extends Spinner {
public MenuSpinner(Context context) {
super(context);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setAdapter(adapter);
setSelection(0, false);
.
.
.
}
}
公共类MenuSpinner扩展微调器{
公共菜单内部(上下文){
超级(上下文);
ArrayAdapter=ArrayAdapter.createFromResource(这个,R.array.comboCasino,android.R.layout.simple\u微调器\u项);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
设置适配器(适配器);
设置选择(0,假);
.
.
.
}
}
然后将其添加到布局中
<com.yourpackage.MenuSpinner
android:id="comboCasino"
.
.
. />
如果需要更多详细信息,请访问创建一个扩展活动类的BaseActivity类,并在其中添加微调器代码。现在,无论何时需要在其他类中使用微调器,请将该类扩展到BaseActivity,并从BaseActivity调用函数来创建微调器。对于微调器类,我应该创建一个新的Java类还是一个Android活动?一个扩展了Android.widget.spinner的简单Java类我不可能为所有其他活动创建一个模板或其他东西吗?你说的模板是什么意思?