为每个Spinner选项打开不同的Activity

时间:2013-12-13 14:05:46

标签: android spinner

我是Android应用程序的新手。

我有一个微调器设置,会有很多不同的选项。

你能帮帮我吗?

我需要使用每个不同的微调器选项打开一个新活动。

我该怎么做?

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:entries="@array/model_numbers"
    android:prompt="@string/model_spinner" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Please select model number below:"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="peter.gosling@email.com"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="5dp"
    android:text="Version 1.0"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

MainActivity.java

package com.example.capitaokipartslist;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

的strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Capita Oki Parts List</string>
<string name="action_settings">Settings</string>
<string name="model_number">Please select your model number:</string>
<string name="model_spinner">Please select your model number:</string>
<string-array name="model_numbers">
    <item >C711</item>
    <item >ES7411</item>
</string-array>

</resources>

1 个答案:

答案 0 :(得分:0)

假设您已经设置了微调器,您可以实现OnItemSelectedListener并做出相应的响应(请修改为您自己的类名等):

public class SpinnerActivity extends Activity implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)

        Intent intent = null;
        //depending on which item has been selected, prepare an appropriate intent
        //this is based on position of the item that has been clicked
        switch(pos) {
            case 0: 
                intent = new Intent(this, ActivityToStart.class);
                break;
            case 1:
                intent = new Intent(this, OtherActivityToStart.class);
                break;
            default:
                break;
        }

        startActivity(intent);

        //or
        //startActivityForResult(intent);
        //check the links for more info
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

话虽如此,看起来你需要掌握基础知识。请查看文档herehere