Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android編程開發之Spinner控件用法實例分析

Android編程開發之Spinner控件用法實例分析

編輯:關於Android編程

本文實例講述了Android編程開發之Spinner控件用法。分享給大家供大家參考,具體如下:

下拉列表 Spinner,Spinner是一個每次只能選擇所有項的一個項的控件。它的項來自於與之相關聯的適配器中。

Spinner的使用,可以極大提高用戶的體驗性。當需要用戶選擇的時候,可以提供一個下拉列表將所有可選的項列出來。供用戶選擇。

一、使用數組作為數據源

布局文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin" 
 tools:context=".MainActivity" > 
 <Spinner 
  android:id="@+id/spinner1" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_alignParentLeft="true" 
  android:layout_alignParentTop="true" /> 
</RelativeLayout>

MainActivity.java

package com.example.lesson16_spinner; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.SimpleAdapter; 
import android.widget.Spinner; 
import android.widget.Toast; 
public class MainActivity extends Activity { 
 // 聲明spinner對象 
 private Spinner spinner; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  spinner = (Spinner) findViewById(R.id.spinner1); 
  //使用數組作為數據源 
  final String arr[] = new String[] { "周一", "周二", "周三", "周四", "周五", "周六", "周七" }; 
  // adpater對象 
  ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr); 
  List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
  Map<String, Object> map = new HashMap<String, Object>(); 
  map.put("logo", R.drawable.ic_launcher); 
  map.put("text", "xxxx說明操作"); 
  Map<String, Object> map2 = new HashMap<String, Object>(); 
  map2.put("logo", R.drawable.ic_launcher); 
  map2.put("text", "xxx說明xxx"); 
  data.add(map); 
  data.add(map2); 
  SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.activity_item, new String[] { "logo", "text" }, new int[] { R.id.imageView1, R.id.textView1 }); 
  // 設置顯示的數據 
   spinner.setAdapter(arrayAdapter); 
  //spinner.setAdapter(simpleAdapter); 
  Toast.makeText(getApplicationContext(),"main Thread"+ spinner.getItemAtPosition(spinner.getSelectedItemPosition()), Toast.LENGTH_LONG).show(); 
  // 注冊事件 
  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
   @Override 
   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
    // String content = arr[position]; 
    Spinner spinner = (Spinner) parent; 
    Toast.makeText(getApplicationContext(), "xxxx" + spinner.getItemAtPosition(position), Toast.LENGTH_LONG).show(); 
   } 
   @Override 
   public void onNothingSelected(AdapterView<?> parent) { 
    Toast.makeText(getApplicationContext(), "沒有改變的處理", Toast.LENGTH_LONG).show(); 
   } 
  }); 
 } 
 @Override 
 public boolean onCreateOptionsMenu(Menu menu) { 
  // Inflate the menu; this adds items to the action bar if it is present. 
  getMenuInflater().inflate(R.menu.main, menu); 
  return true; 
 } 
}

效果如下:

 

二、使用XML作為數據源(非本人代碼)

布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 <string-array name="plantes"> 
  <item>NOKIA</item> 
  <item>MOTO</item> 
  <item>HTC</item> 
  <item>LG</item> 
  <item>其他</item> 
 </string-array> 
</resources>

java代碼:

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.TextView; 
public class SpinnerActivity extends Activity { 
 private TextView view2; 
 private Spinner spinner2; 
 private ArrayAdapter adapter2; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  // TODO Auto-generated method stub 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.spinner); 
  spinner2 = (Spinner) findViewById(R.id.spinner02); 
  view2 = (TextView) findViewById(R.id.spinnerText02); 
  //將可選內容與ArrayAdapter連接起來 
  adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item); 
  //設置下拉列表的風格 
  adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
  //將adapter2 添加到spinner中 
  spinner2.setAdapter(adapter2); 
  //添加事件Spinner事件監聽 
  spinner2.setOnItemSelectedListener(new SpinnerXMLSelectedListener());
  //設置默認值 
  spinner2.setVisibility(View.VISIBLE); 
 } 
 //使用XML形式操作 
 class SpinnerXMLSelectedListener implements OnItemSelectedListener{ 
  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
   view2.setText("你使用什麼樣的手機:"+adapter2.getItem(arg2)); 
  } 
  public void onNothingSelected(AdapterView<?> arg0) { 
  } 
 } 
}

希望本文所述對大家Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved