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

(0)

相关推荐

  • Android中Spinner控件之键值对用法实例分析

    本文实例讲述了Android中Spinner控件之键值对用法.分享给大家供大家参考.具体如下: 一.字典表,用来存放键值对信息 package com.ljq.activity; import java.io.Serializable; @SuppressWarnings("serial") public class Dict implements Serializable { private Integer id; private String text; public Dict()

  • Android UI组件Spinner下拉列表详解

    Spinner下拉列表 该布局对应的关系图: 常用属性:android:entries(指定spinner要显示的字符串资源.必须是在strings资源文件中定义的字符串资源)android:spinnerMode(spinner的模式,枚举值有两个值dialog弹窗显示和dropdown下拉显示)android:dropDownWidth(下拉框的宽度,单位通常是dp)android:prompt(当spinnerMode的值是dialog时,弹出的对话框式的下列列表的提示.如果 spinne

  • Android Spinner 下拉菜单的使用

    Android 中下拉菜单,即如html中的<select>,关键在于调用setDropDownViewResource方法,以XML的方式定义下拉菜单要显示的模样 步骤: 1.定义Spinner控件 复制代码 代码如下: <Spinner android:id="@+id/spinner"android:layout_width="fill_parent"android:layout_height="wrap_content"

  • Android自定义Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)

    今天学习了Spinner组件的使用,非常好用的一款组件,相当于从下拉列表中选择项目,今天收获颇多,下面给大家演示一下Spinner的使用(分别使用ArrayAdapter和自定义Adapter实现),具体内容如下. (一):使用ArrayAdapter进行适配数据: ①:首先定义一个布局文件: <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?&

  • Android中Spinner(下拉框)控件的使用详解

    android给我们提供了一个spinner控件,这个控件主要就是一个列表,那么我们就来说说这个控件吧,这个控件在以前的也看见过,但今天还是从新介绍一遍吧. Spinner位于 android.widget包下,每次只显示用户选中的元素,当用户再次点击时,会弹出选择列表供用户选择,而选择列表中的元素同样来自适配器.Spinner是View类得一个子类. 1.效果图 2.创建页面文件(main.xml) <Spinner android:id="@+id/spinner1" and

  • Android下拉列表spinner的实例代码

    spinner组件有点类型于HTML中的下拉框<Select></select>的样子,让用户每次从下拉框中选取一个,本文为大家分享了Android下拉列表spinner的具体实现代码,供大家参考,具体内容如下 mian.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.c

  • Android控件Spinner的使用方法(1)

    一.使用方法 1.在layout中创建Spinner控件 <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" /> 2.给数据适配器添加数据源和显示格式 String[] city=new String[]{"北京","上海",&q

  • Android实现下拉菜单Spinner效果

    Android 中下拉菜单,即如html中的<select>,关键在于调用setDropDownViewResource方法,以XML的方式定义下拉菜单要显示的模样 1.1.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android

  • Android实现三级联动下拉框 下拉列表spinner的实例代码

    主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值              XML布局: 复制代码 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_w

  • android应用开发之spinner控件的简单使用

    Android的控件有很多种,其中就有一个Spinner的控件,这个控件其实就是一个下拉显示列表.Spinner是位于 android.widget包下的,每次只显示用户选中的元素,当用户再次点击时,会弹出选择列表供用户选择,而选择列表中的元素同样来自适配器.Spinner是View类的一个子类. 先看spinner的效果图: 代码: MainActivity package com.mecury.spinnertest; import java.util.ArrayList; import a

随机推荐