如何在工具栏上的下拉菜单中选择的项目上打开不同的活动

时间:2015-03-05 17:21:38

标签: android drop-down-menu android-spinner onitemselectedlistener

我在互联网上搜索过此内容,但未找到合适的答案。我正在我的工具栏上加载一个微调器下拉菜单,其中包含在被选中后打开不同活动的项目。到目前为止,我已经能够创建一个返回所选项目名称的toast。但是我想为菜单上的每个项目打开不同的活动。我知道这可能是某个地方的重复,但他们的答案对我来说还不起作用。 这是我的微调器:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="2dp"
    android:background="@color/primaryColor"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">
    <Spinner
        android:id="@+id/spinner_nav"
        android:layout_width="wrap_content"
        android:gravity="bottom"
        android:layout_height="wrap_content"></Spinner>

</android.support.v7.widget.Toolbar>

这是我的MainActivity.java

public class MainActivity extends ActionBarActivity {

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);


        final Spinner spinner_navs = (Spinner) findViewById(R.id.spinner_nav);

        SpinnerAdapter mSpinner = ArrayAdapter.createFromResource
                (this, R.array.action_bar_spinner, android.R.layout.simple_spinner_dropdown_item);
        spinner_navs.setAdapter(mSpinner);
        spinner_navs.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String item = parent.getItemAtPosition(position).toString();
                    Toast.makeText(parent.getContext(), item, Toast.LENGTH_LONG).show();

                }


            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

我知道我是新手,但是对此有任何帮助我会很感激。提前谢谢。

2 个答案:

答案 0 :(得分:2)

使用此

spinner_nav.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> adapter, View v,
     int position, long id) {
    // On selecting a spinner item

    if(position==0){
        // open activity 1
    }else if(position==1){
        // open activity 2
    }
    else if(position==2){
        // open activity 3
    }
   }

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

   }
  });

答案 1 :(得分:1)

使用Switch语句

 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String item = parent.getItemAtPosition(position).toString();
    switch (position) {

                case 1:

            Intent intent = new Intent(this, ResultActivity.class);
            intent.putExtra("yourkeyone", string);
                    break;

                case 2:

            Intent i = new Intent(this, ResultActivity.class);
            i.putExtra("yourkeytwo", string);
                    break;

                   //ect...
}   
return true;
}