服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android 之Spinner下拉菜单实现级联

android 之Spinner下拉菜单实现级联

2021-01-07 15:00Android开发网 Android

android 之Spinner下拉菜单实现级联,需要的朋友可以参考一下

效果图如下:

默认第一次加载

android 之Spinner下拉菜单实现级联

选择原始队列:

android 之Spinner下拉菜单实现级联

级联效果图:

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;
 }
}

 

延伸 · 阅读

精彩推荐