第21章、OnItemSelectedListener事件(从零开始学Android)

Android App应用中,OnItemSelectedListener事件也会经常用到,我们一起来了解一下。

  基本知识点:OnItemSelectedListener事件

一、界面

   1、新建province.xml件。

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

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

  

  (2)province.xml内容如下:  

  

  (3)代码  

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>     
  3.     <string-array name="provarray">  
  4.         <item>河南省</item>  
  5.         <item>河北省</item>  
  6.         <item>山东省</item>  
  7.         <item>山西省</item>  
  8.     </string-array>      
  9. </resources>  

  2、打开“res/layout/activity_main.xml”文件。

   (1)分别从工具栏向activity拖出1个下拉列表框Spinner。控件来自Form Widgets。

  

  (2)打开activity_main.xml文件。  

[html] view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Spinner  
  8.         android:id="@+id/province"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:entries="@array/provarray" />  
  14.   
  15. </RelativeLayout>  

  3、界面如下

  

二、OnItemSelectedListener事件 

  1、打开“src/com.genwoxue.onitemselected/MainActivity.java”文件。

  然后输入以下代码:

[java] view plain copy
  1. package com.genwoxue.onitemselected;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.View;  
  6. import android.widget.Spinner;  
  7. import android.widget.Toast;  
  8. import android.widget.AdapterView;  
  9. import android.widget.AdapterView.OnItemSelectedListener;  
  10.   
  11.   
  12. public class MainActivity extends Activity {  
  13.     //声明Spinner对象  
  14.     private Spinner spinProvince=null;  
  15.       
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         //获取Spinner  
  21.         spinProvince=(Spinner)super.findViewById(R.id.province);  
  22.         //注册OnItemSelected事件  
  23.         spinProvince.setOnItemSelectedListener(new ProvOnItemSelectedListener());  
  24.         }  
  25.   
  26.     //OnItemSelected监听器  
  27.     private class  ProvOnItemSelectedListener implements OnItemSelectedListener{          
  28.         @Override  
  29.         public void onItemSelected(AdapterView<?> adapter,View view,int position,long id) {  
  30.             //获取选择的项的值  
  31.             String sInfo=adapter.getItemAtPosition(position).toString();  
  32.             Toast.makeText(getApplicationContext(), sInfo, Toast.LENGTH_LONG).show();             
  33.         }  
  34.   
  35.         @Override  
  36.         public void onNothingSelected(AdapterView<?> arg0) {  
  37.             String sInfo="什么也没选!";  
  38.             Toast.makeText(getApplicationContext(),sInfo, Toast.LENGTH_LONG).show();  
  39.               
  40.         }  
  41.     }  
  42. }  


 

  2、最终效果如下:

  

原文地址:https://www.cnblogs.com/wanghuaijun/p/6049949.html