OnItemSelectedListener事件与二级联动

一、界面

   1、新建province.xml件。

  在“res/values”位置新建province.xml文件。

  (1)province.xml文件位置如下图所示:

  

  (2)province.xml内容如下:  

  

  (3)代码  

  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <resources>

  3. <string-arrayname="provarray">

  4. <item>河南省</item>

  5. <item>河北省</item>

  6. <item>山东省</item>

  7. <item>山西省</item>

  8. </string-array>

  9. </resources>

      (4)main_activity布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/liuyifei"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/province"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/province"
       
       />

    <Spinner
        android:id="@+id/city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    
        />

</LinearLayout>

     
               (5)Activity(src下的Java代码)

 

package com.example.item2jiliandong;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

       //声明Spinner对像  
        private Spinner spinProvince=null;  
        private Spinner spinCity=null;  
        //定义城市数据,用于二级菜单  
        private String[][] arrCity=new String[][]{  
            {"郑州","开封","洛阳","安阳"},  
            {"石家庄","保定","邯郸","张家口"},  
            {"济南","青岛","烟台","日照"},  
            {"太原","晋中","吕梁","临汾"}};  
        //声明数组适配器,用来填充城市列表  
        private ArrayAdapter<CharSequence> adapterCity=null;  
          
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            //获取Spinner对象  
            spinProvince=(Spinner)super.findViewById(R.id.province);  
            spinCity=(Spinner)super.findViewById(R.id.city);  
            //为spinProvince控件注册OnItemSelected监听器  
            spinProvince.setOnItemSelectedListener(new ProvOnItemSelectedListener());  
           //为spinCity控件注册OnItemSelected监听器  
            spinCity.setOnItemSelectedListener(new CityOnItemSelectedListener());  
            }  
      
        //OnItemSelected监听器  
        private class  ProvOnItemSelectedListener implements OnItemSelectedListener{      
            //选择省份,触发城市下拉列表框  
            @Override  
            public void onItemSelected(AdapterView<?> adapter,View view,int position,long id) {  
               //使用ArrayAdapter转换数据  
               MainActivity.this.adapterCity=new ArrayAdapter<CharSequence>(  
                       MainActivity.this,  
                        android.R.layout.simple_spinner_item,  
                       MainActivity.this.arrCity[position]);  
                //使用adapterCity数据适配器填充spinCity  
               MainActivity.this.spinCity.setAdapter(MainActivity.this.adapterCity);  
              
            }  
    
            @Override  
            public void onNothingSelected(AdapterView<?> arg0) {  
                //没有选择时执行  
            }  
       }  
        //OnItemSelected监听器  
        private class CityOnItemSelectedListener implements OnItemSelectedListener{      
            //选择城市,触发显示选择的城市  
            @Override    
            public void onItemSelected(AdapterView<?> adapter,View view,int position,long id) {    
                //获取选择的项的值     
                String sInfo=adapter.getItemAtPosition(position).toString();    
                Toast.makeText(getApplicationContext(), sInfo, Toast.LENGTH_LONG).show();  //MainActivity.this等价于 getApplicationContext()            
            }      
            @Override    
            public void onNothingSelected(AdapterView<?> arg0) {    
                //没有选择时执行                
            }    
        }    


        
        
    }

    

               (6)运行结果 

               

       运行运用程序所得结果如下图:

      

      

               

 

posted on 2013-08-08 10:20  jianrong.zheng  阅读(361)  评论(0编辑  收藏  举报

导航