2013-12-13 27 views
-2

我對Android應用程序非常陌生。爲每個Spinner選項打開不同的活動

我有一個微調設置,會有很多不同的選項。

你能幫我嗎?

我需要爲每個不同的微調器選項打開一個新的活動。

我該怎麼做?

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="[email protected]" 
    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> 
+0

張貼您的代碼 – Raghunandan

+0

是否發佈您的代碼.. –

+0

@raghunandan添加上面的代碼 –

回答

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

相關問題