android 之 Spinner 下拉選單實現級聯

yangxi_001發表於2014-03-31

效果圖如下:

預設第一次載入

選擇原始佇列:

級聯效果圖:

 

 關鍵程式碼給下拉選單選中事件監聽繫結Id :

[html] view plaincopy
  1. int pos = firsthand_dlbh_pinner.getSelectedItemPosition();  
  2. firsthand_pdbh_adapter = new ArrayAdapter<String>(context,  
  3.         android.R.layout.simple_spinner_item, queu_info[pos]);  
  4. firsthand_pdbh_spinner.setAdapter(firsthand_pdbh_adapter);  


 

完整程式碼:

佈局檔案程式碼:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/firsthand_dlbh"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginLeft="60sp"  
  12.         android:layout_marginTop="20sp" >  
  13.     </TextView>  
  14.   
  15.     <Spinner  
  16.         android:id="@+id/firsthand_dlbh_pinner"  
  17.         android:layout_width="200sp"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_marginLeft="60sp"  
  20.         android:layout_marginTop="20sp" >  
  21.     </Spinner>  
  22.       
  23.     <TextView  
  24.         android:id="@+id/firsthand_pdbh"  
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_marginLeft="60sp"  
  28.         android:layout_marginTop="20sp" >  
  29.     </TextView>  
  30.   
  31.     <Spinner  
  32.         android:id="@+id/firsthand_pdbh_pinner"  
  33.         android:layout_width="200sp"  
  34.         android:layout_height="wrap_content"  
  35.         android:layout_marginLeft="60sp"  
  36.         android:layout_marginTop="20sp" >  
  37.     </Spinner>  
  38.   
  39.     <TextView  
  40.         android:id="@+id/target_dlbh"  
  41.         android:layout_width="fill_parent"  
  42.         android:layout_height="wrap_content"  
  43.         android:layout_marginLeft="60sp"  
  44.         android:layout_marginTop="20sp" >  
  45.     </TextView>  
  46.   
  47.     <Spinner  
  48.         android:id="@+id/target_dlbh_spinner"  
  49.         android:layout_width="200sp"  
  50.         android:layout_height="wrap_content"  
  51.         android:layout_marginLeft="60sp"  
  52.         android:layout_marginTop="20sp" >  
  53.     </Spinner>  
  54.   
  55.     <Button  
  56.         android:id="@+id/diff_manage_operation"  
  57.         android:layout_width="wrap_content"  
  58.         android:layout_height="wrap_content"  
  59.         android:layout_marginLeft="100sp"  
  60.         android:layout_marginTop="20sp"  
  61.         android:text="執行排程"  
  62.         android:textSize="18sp" >  
  63.     </Button>  
  64.   
  65. </LinearLayout>  


JAVA 程式程式碼:

[java] view plaincopy
  1. import android.app.Activity;  
  2. import android.content.Context;  
  3.   
  4. import android.os.Bundle;  
  5.   
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.AdapterView;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.Button;  
  12. import android.widget.Spinner;  
  13. import android.widget.TextView;  
  14. import android.widget.AdapterView.OnItemSelectedListener;  
  15.   
  16. public class ManageInfo_Activity extends Activity implements OnClickListener {  
  17.     private Context context;  
  18.     private static final String[] queuilist = { "A999""B888" };  
  19.     private static final String[] default_linfo = { "A01""A02""A03""A04",  
  20.             "A05""A06""A07""A08""A09""A10""A11" };  
  21.     private static final String[][] queu_info = new String[][] {  
  22.             { "A01""A02""A03""A04""A05""A06""A07""A08""A09",  
  23.                     "A10""A11" },  
  24.             { "B01""B02""B03""B04""B05""B06""B07""B08""B09",  
  25.                     "B10""B11" } };  
  26.     private TextView firsthand_dlbh_view;  
  27.     private Spinner firsthand_dlbh_pinner;  
  28.     private ArrayAdapter<String> firsthand_dlbh_adapter;  
  29.   
  30.     private TextView firsthand_pdbh_view;  
  31.     private Spinner firsthand_pdbh_spinner;  
  32.     private ArrayAdapter<String> firsthand_pdbh_adapter;  
  33.   
  34.     private TextView target_dlbh_view;  
  35.     private Spinner target_dlbh_spinner;  
  36.     private ArrayAdapter<String> target_dlbh_adapter;  
  37.   
  38.     private String firsthand_dlbh = "";  
  39.     private String firsthand_pdbh = "";  
  40.     private String target_dlbh = "";  
  41.   
  42.     @Override  
  43.     protected void onCreate(Bundle savedInstanceState) {  
  44.         // TODO Auto-generated method stub  
  45.         super.onCreate(savedInstanceState);  
  46.         setContentView(R.layout.activity_manageinfo);  
  47.   
  48.         context = this;  
  49.   
  50.         // 定義原始佇列編號下拉選單  
  51.         firsthand_dlbh_view = (TextView) findViewById(R.id.firsthand_dlbh);  
  52.         firsthand_dlbh_pinner = (Spinner) findViewById(R.id.firsthand_dlbh_pinner);  
  53.         // 將可選內容與ArrayAdapter連線起來  
  54.         firsthand_dlbh_adapter = new ArrayAdapter<String>(this,  
  55.                 android.R.layout.simple_spinner_item, queuilist);  
  56.         // 設定下拉選單的風格  
  57.         firsthand_dlbh_adapter  
  58.                 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  59.         // 將adapter 新增到spinner中  
  60.         firsthand_dlbh_pinner.setAdapter(firsthand_dlbh_adapter);  
  61.         // 新增事件Spinner事件監聽  
  62.         firsthand_dlbh_pinner  
  63.                 .setOnItemSelectedListener(new FirsthanddlbhSpinnerSelectedListener());  
  64.         // 設定預設值  
  65.         firsthand_dlbh_pinner.setVisibility(View.VISIBLE);  
  66.   
  67.         // 定義原始排隊編號下拉選單  
  68.         firsthand_pdbh_view = (TextView) findViewById(R.id.firsthand_pdbh);  
  69.         firsthand_pdbh_spinner = (Spinner) findViewById(R.id.firsthand_pdbh_pinner);  
  70.         // 將可選內容與ArrayAdapter連線起來  
  71.         firsthand_pdbh_adapter = new ArrayAdapter<String>(this,  
  72.                 android.R.layout.simple_spinner_item, default_linfo);  
  73.         // 設定下拉選單的風格  
  74.         firsthand_pdbh_adapter  
  75.                 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  76.         // 將adapter 新增到spinner中  
  77.         firsthand_pdbh_spinner.setAdapter(firsthand_pdbh_adapter);  
  78.         // 新增事件Spinner事件監聽  
  79.         firsthand_pdbh_spinner  
  80.                 .setOnItemSelectedListener(new FirsthandpdbhSpinnerSelectedListener());  
  81.         // 設定預設值  
  82.         firsthand_pdbh_spinner.setVisibility(View.VISIBLE);  
  83.   
  84.         // 定義目標佇列下拉選單  
  85.         target_dlbh_view = (TextView) findViewById(R.id.target_dlbh);  
  86.         target_dlbh_spinner = (Spinner) findViewById(R.id.target_dlbh_spinner);  
  87.         // 將可選內容與ArrayAdapter連線起來  
  88.         target_dlbh_adapter = new ArrayAdapter<String>(this,  
  89.                 android.R.layout.simple_spinner_item, queuilist);  
  90.         // 設定下拉選單的風格  
  91.         target_dlbh_adapter  
  92.                 .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
  93.         // 將adapter 新增到spinner中  
  94.         target_dlbh_spinner.setAdapter(target_dlbh_adapter);  
  95.         // 新增事件Spinner事件監聽  
  96.         target_dlbh_spinner  
  97.                 .setOnItemSelectedListener(new TargetdlbhpinnerSelectedListener());  
  98.         // 設定預設值  
  99.         target_dlbh_spinner.setVisibility(View.VISIBLE);  
  100.   
  101.         Button diff_manage_operation = (Button) findViewById(R.id.diff_manage_operation);  
  102.         diff_manage_operation.setOnClickListener(this);  
  103.   
  104.     }  
  105.   
  106.     // 選擇 目標排隊編號 事件 監聽器  
  107.     class FirsthanddlbhSpinnerSelectedListener implements  
  108.             OnItemSelectedListener {  
  109.   
  110.         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  111.                 long arg3) {  
  112.             firsthand_dlbh = queuilist[arg2];  
  113.             firsthand_dlbh_view.setText("選擇原始佇列是:" + queuilist[arg2]);  
  114.             int pos = firsthand_dlbh_pinner.getSelectedItemPosition();  
  115.             firsthand_pdbh_adapter = new ArrayAdapter<String>(context,  
  116.                     android.R.layout.simple_spinner_item, queu_info[pos]);  
  117.             firsthand_pdbh_spinner.setAdapter(firsthand_pdbh_adapter);  
  118.   
  119.         }  
  120.   
  121.         public void onNothingSelected(AdapterView<?> arg0) {  
  122.         }  
  123.     }  
  124.   
  125.     // 選擇 目標排隊編號 事件 監聽器  
  126.     class FirsthandpdbhSpinnerSelectedListener implements  
  127.             OnItemSelectedListener {  
  128.   
  129.         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  130.                 long arg3) {  
  131.             firsthand_pdbh = (String) firsthand_pdbh_spinner  
  132.                     .getItemAtPosition(arg2);  
  133.             firsthand_pdbh_view.setText("要移動的排隊編號是:"  
  134.                     + (String) firsthand_pdbh_spinner.getItemAtPosition(arg2));  
  135.         }  
  136.   
  137.         public void onNothingSelected(AdapterView<?> arg0) {  
  138.         }  
  139.     }  
  140.   
  141.     // //選擇 參照排隊編號 事件 監聽器  
  142.     class TargetdlbhpinnerSelectedListener implements OnItemSelectedListener {  
  143.   
  144.         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
  145.                 long arg3) {  
  146.             target_dlbh = queuilist[arg2];  
  147.             target_dlbh_view.setText("移動到 " + queuilist[arg2] + " 佇列中 ");  
  148.         }  
  149.   
  150.         public void onNothingSelected(AdapterView<?> arg0) {  
  151.         }  
  152.     }  
  153.   
  154.   
  155.     @Override  
  156.     public void onClick(View v) {  
  157.   
  158.         System.out.println(" 進入同一佇列排程環節");  
  159.   
  160.         System.out.println("執行把排隊編號 " + firsthand_pdbh + " 從 " + firsthand_dlbh  
  161.                 + " 佇列中移動到  " + target_dlbh + "   佇列的末尾操作  ");  
  162.     //  DiffMangerQuening( user_name, estination_code, firsthand_dlbh, firsthand_dlbh,target_dlbh); // switch(v.getId()){//根據點選不同的按鈕,彈出相應的提示  
  163.   
  164.   
  165.     }  
  166.   
  167.     @Override  
  168.     public boolean onCreateOptionsMenu(Menu menu) {  
  169.         // Inflate the menu; this adds items to the action bar if it is present.  
  170.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  171.         return true;  
  172.     }  
  173. }  

相關文章