android 之 Spinner 下拉選單實現級聯
效果圖如下:
預設第一次載入
選擇原始佇列:
級聯效果圖:
關鍵程式碼給下拉選單選中事件監聽繫結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;
- }
- }
相關文章
- Android實現三級聯動下拉框 下拉選單spinner的例項程式碼Android
- Spinner下拉選單
- JS實現級聯下拉選單JS
- jquery實現四級級聯下拉選單jQuery
- Android UI控制元件系列:Spinner(下拉選單)AndroidUI控制元件
- 淘寶頁面實現(包括級聯選單,下拉選單,框架)框架
- select下拉選單實現分類級聯效果
- css實現的二級下拉選單效果CSS
- select下拉選單二級聯動
- Android Spinner(級聯 天氣預報)Android
- javascript實現的三級下拉導航選單JavaScript
- jquery實現的點選二級下拉導航選單jQuery
- jQuery結合PHP+MySQL實現二級聯動下拉選單[例項]jQueryPHPMySql
- 出生日期三級聯動下拉選單
- 【Android初級】如何實現一個有動畫效果的自定義下拉選單Android動畫
- jQuery + jQuery Mobile 實現省市二級下拉選單頁面jQuery
- Asp.net+Xml+js實現無線級下拉選單ASP.NETXMLJS
- select下拉選單多級級聯效果程式碼例項
- js物件導向封裝級聯下拉選單列表JS物件封裝
- select下拉選單級聯效果例項程式碼
- select級聯下拉選單程式碼例項分析
- CSS 二級下拉選單CSS
- CSS二級下拉選單CSS
- jQuery 二級下拉選單jQuery
- HTML+CSS實現下拉選單HTMLCSS
- 純CSS二級下拉導航選單實CSS
- 實現基於json的級聯選單JSON
- Vue.js自定義下拉選單,如何實現在下拉選單區域外點選即可關閉下拉選單的功能Vue.js
- JavaScript讀取xml實現下拉選單JavaScriptXML
- checkbox及css實現點選下拉選單CSS
- 選擇下拉選單項實現跳轉效果
- 純CSS實現的二級下拉選單效果程式碼例項CSS
- jq+php+mysql 實現二級選單聯動PHPMySql
- C#實現的下拉多選框,下拉多選樹,多級節點C#
- 使用Vue實現下拉選單框批量新增選項Vue
- Vue 資料雙向繫結實現二級下拉懸浮選單Vue
- Android-Spinner [使用C# And Java實現]AndroidC#Java
- JavaScript二級下拉選單詳解JavaScript