Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207920
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-11 20:31:56

02_01_spinner的使用

布局activity_main.xml

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10. <Spinner
  11.     android:id="@+id/spinnerId"
  12.     android:layout_width="fill_parent"
  13.     android:layout_height="wrap_content"/>
  14. </RelativeLayout>

Strings.xml文件

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>

  3.     <string name="app_name">spinner</string>
  4.     <string name="action_settings">Settings</string>
  5.     <string name="hello_world">Hello world!</string>
  6.     
  7.     <string-array name="cities_array">
  8.         <item>北京</item>
  9.         <item>广州</item>
  10.         <item>深圳</item>
  11.         <item>上海</item>
  12.         <item >香港</item>
  13.         <item>成都</item>
  14.     </string-array>
  15. </resources>





item.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <TextView
  8.         android:id="@+id/textViewId"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         />
  12. </LinearLayout>




点击(此处)折叠或打开

  1. //mainActivity.java
  2. package com.lwb.spinner;

  3. import java.util.ArrayList;
  4. import java.util.List;

  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.view.Menu;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemSelectedListener;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.Spinner;
  14. import android.widget.Toast;

  15. public class MainActivity extends Activity {
  16.     private Spinner spinner = null;    
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);        
  21.         spinner = (Spinner)findViewById(R.id.spinnerId);            
  22.         /*1、通过createFromResource方法创建ArrayAdater对象
  23.          * adapter = ArrayAdapter.createFromResource(this
  24.                 , R.array.planets_array,android.R.layout.simple_spinner_item);
  25.          * this:表上下文对象
  26.          * R.array.planets_arry:表引用了在strings.xml文件当红定义的的String数组
  27.          * android.R.layout.simple_spinner_dropdown_item:用于指定Spinner的样式,是一个布局文件的ID,该布局
  28.          * 有Android系统提供,也可以替换为自定义的布局文件
  29.          * */            
  30.         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this
  31.                 , R.array.cities_array,android.R.layout.simple_spinner_item);        
  32.         /*2、设置Spinner当中每一个条目的样式,同样是引用Android系统提供的布局文件*/
  33.         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    
  34.         
  35.         /*22、*/
  36.         List<String> list = new ArrayList<String>();
  37.         list.add("纽约");
  38.         list.add("旧金山");        
  39.     //    ArrayAdapter adapter = new ArrayAdapter(this,R.layout.item,R.id.textViewId,list);
  40.         
  41.         /*3、为spinner设置Adater*/
  42.         spinner.setAdapter(adapter);        
  43.         /*4、为spinner的标题*/
  44.         spinner.setPrompt("城市");        
  45.         /*5/为spinner对象绑定监听器*/
  46.         spinner.setOnItemSelectedListener(new SpinnerOnSelectedListenner());        
  47.     }
  48.     
  49.      class SpinnerOnSelectedListenner implements OnItemSelectedListener{
  50.         @Override
  51.         public void onItemSelected(AdapterView<?> adapterView, View view, int position,
  52.                 long id) {
  53.             System.out.println("onItemSelected*******");
  54.             String selected = adapterView.getItemAtPosition(position).toString();
  55.             System.out.println(selected);
  56.             Toast.makeText(MainActivity.this, selected , Toast.LENGTH_SHORT).show();            
  57.         }

  58.         @Override
  59.         public void onNothingSelected(AdapterView<?> arg0) {
  60.             System.out.println("nothingSelected*********");    
  61.             Toast.makeText(MainActivity.this, "nothingSelected*********" , Toast.LENGTH_SHORT).show();
  62.         }        
  63.     }
  64.  }

阅读(832) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~