Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 之Spinner下拉菜單實現級聯

android 之Spinner下拉菜單實現級聯

編輯:關於Android編程

效果圖如下:

默認第一次加載

選擇原始隊列:

級聯效果圖:

關鍵代碼給下拉列表選中事件監聽綁定Id :
復制代碼 代碼如下:
int pos = firsthand_dlbh_pinner.getSelectedItemPosition();
firsthand_pdbh_adapter = new ArrayAdapter<String>(context,
 android.R.layout.simple_spinner_item, queu_info[pos]);
firsthand_pdbh_spinner.setAdapter(firsthand_pdbh_adapter);

完整代碼:

布局文件代碼:

復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/firsthand_dlbh"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </TextView>

    <Spinner
        android:id="@+id/firsthand_dlbh_pinner"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </Spinner>

    <TextView
        android:id="@+id/firsthand_pdbh"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </TextView>

    <Spinner
        android:id="@+id/firsthand_pdbh_pinner"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </Spinner>

    <TextView
        android:id="@+id/target_dlbh"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </TextView>

    <Spinner
        android:id="@+id/target_dlbh_spinner"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60sp"
        android:layout_marginTop="20sp" >
    </Spinner>

    <Button
        android:id="@+id/diff_manage_operation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100sp"
        android:layout_marginTop="20sp"
        android:text="執行調度"
        android:textSize="18sp" >
    </Button>

</LinearLayout>

JAVA 程序代碼:
復制代碼 代碼如下:
import android.app.Activity;
import android.content.Context;

import android.os.Bundle;

import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class ManageInfo_Activity extends Activity implements OnClickListener {
 private Context context;
 private static final String[] queuilist = { "A999", "B888" };
 private static final String[] default_linfo = { "A01", "A02", "A03", "A04",
   "A05", "A06", "A07", "A08", "A09", "A10", "A11" };
 private static final String[][] queu_info = new String[][] {
   { "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09",
     "A10", "A11" },
   { "B01", "B02", "B03", "B04", "B05", "B06", "B07", "B08", "B09",
     "B10", "B11" } };
 private TextView firsthand_dlbh_view;
 private Spinner firsthand_dlbh_pinner;
 private ArrayAdapter<String> firsthand_dlbh_adapter;

 private TextView firsthand_pdbh_view;
 private Spinner firsthand_pdbh_spinner;
 private ArrayAdapter<String> firsthand_pdbh_adapter;

 private TextView target_dlbh_view;
 private Spinner target_dlbh_spinner;
 private ArrayAdapter<String> target_dlbh_adapter;

 private String firsthand_dlbh = "";
 private String firsthand_pdbh = "";
 private String target_dlbh = "";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_manageinfo);

  context = this;

  // 定義原始隊列編號下拉菜單
  firsthand_dlbh_view = (TextView) findViewById(R.id.firsthand_dlbh);
  firsthand_dlbh_pinner = (Spinner) findViewById(R.id.firsthand_dlbh_pinner);
  // 將可選內容與ArrayAdapter連接起來
  firsthand_dlbh_adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, queuilist);
  // 設置下拉列表的風格
  firsthand_dlbh_adapter
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  // 將adapter 添加到spinner中
  firsthand_dlbh_pinner.setAdapter(firsthand_dlbh_adapter);
  // 添加事件Spinner事件監聽
  firsthand_dlbh_pinner
    .setOnItemSelectedListener(new FirsthanddlbhSpinnerSelectedListener());
  // 設置默認值
  firsthand_dlbh_pinner.setVisibility(View.VISIBLE);

  // 定義原始排隊編號下拉菜單
  firsthand_pdbh_view = (TextView) findViewById(R.id.firsthand_pdbh);
  firsthand_pdbh_spinner = (Spinner) findViewById(R.id.firsthand_pdbh_pinner);
  // 將可選內容與ArrayAdapter連接起來
  firsthand_pdbh_adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, default_linfo);
  // 設置下拉列表的風格
  firsthand_pdbh_adapter
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  // 將adapter 添加到spinner中
  firsthand_pdbh_spinner.setAdapter(firsthand_pdbh_adapter);
  // 添加事件Spinner事件監聽
  firsthand_pdbh_spinner
    .setOnItemSelectedListener(new FirsthandpdbhSpinnerSelectedListener());
  // 設置默認值
  firsthand_pdbh_spinner.setVisibility(View.VISIBLE);

  // 定義目標隊列下拉菜單
  target_dlbh_view = (TextView) findViewById(R.id.target_dlbh);
  target_dlbh_spinner = (Spinner) findViewById(R.id.target_dlbh_spinner);
  // 將可選內容與ArrayAdapter連接起來
  target_dlbh_adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, queuilist);
  // 設置下拉列表的風格
  target_dlbh_adapter
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  // 將adapter 添加到spinner中
  target_dlbh_spinner.setAdapter(target_dlbh_adapter);
  // 添加事件Spinner事件監聽
  target_dlbh_spinner
    .setOnItemSelectedListener(new TargetdlbhpinnerSelectedListener());
  // 設置默認值
  target_dlbh_spinner.setVisibility(View.VISIBLE);

  Button diff_manage_operation = (Button) findViewById(R.id.diff_manage_operation);
  diff_manage_operation.setOnClickListener(this);

 }

 // 選擇 目標排隊編號 事件 監聽器
 class FirsthanddlbhSpinnerSelectedListener implements
   OnItemSelectedListener {

  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
   firsthand_dlbh = queuilist[arg2];
   firsthand_dlbh_view.setText("選擇原始隊列是:" + queuilist[arg2]);
   int pos = firsthand_dlbh_pinner.getSelectedItemPosition();
   firsthand_pdbh_adapter = new ArrayAdapter<String>(context,
     android.R.layout.simple_spinner_item, queu_info[pos]);
   firsthand_pdbh_spinner.setAdapter(firsthand_pdbh_adapter);

  }

  public void onNothingSelected(AdapterView<?> arg0) {
  }
 }

 // 選擇 目標排隊編號 事件 監聽器
 class FirsthandpdbhSpinnerSelectedListener implements
   OnItemSelectedListener {

  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
   firsthand_pdbh = (String) firsthand_pdbh_spinner
     .getItemAtPosition(arg2);
   firsthand_pdbh_view.setText("要移動的排隊編號是:"
     + (String) firsthand_pdbh_spinner.getItemAtPosition(arg2));
  }

  public void onNothingSelected(AdapterView<?> arg0) {
  }
 }

 // //選擇 參照排隊編號 事件 監聽器
 class TargetdlbhpinnerSelectedListener implements OnItemSelectedListener {

  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
   target_dlbh = queuilist[arg2];
   target_dlbh_view.setText("移動到 " + queuilist[arg2] + " 隊列中 ");
  }

  public void onNothingSelected(AdapterView<?> arg0) {
  }
 }


 @Override
 public void onClick(View v) {

  System.out.println(" 進入同一隊列調度環節");

  System.out.println("執行把排隊編號 " + firsthand_pdbh + " 從 " + firsthand_dlbh
    + " 隊列中移動到  " + target_dlbh + "   隊列的末尾操作  ");
 // DiffMangerQuening( user_name, estination_code, firsthand_dlbh, firsthand_dlbh,target_dlbh); // switch(v.getId()){//根據點擊不同的按鈕,彈出相應的提示


 }

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved