Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android Spinner實現下拉列表

Android Spinner實現下拉列表

編輯:高級開發

一部分轉自http://blog.csdn.Net/dadahacker/archive/2010/07/17/5741865.ASPx

  第一個demo

  XML文件:聲明一個TextVIEw控件和一個Spinner控件

  Java代碼 收藏代碼

  < ?XML version="1.0" encoding="utf-8"?>

  < LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"

  android:orIEntation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

  < TextVIEw

  android:text="@+id/TextVIEw01"

  android:id="@+id/TextVIEw01"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content">

  < /TextVIEw>

  < Spinner android:id="@+id/Spinner01"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content">< /Spinner>

  < /LinearLayout>

  主代碼為:

  Java代碼 收藏代碼

  package com.test;

  import android.app.Activity;

  import android.os.Bundle;

  import android.vIEw.*;

  import android.widget.*;

  public class test extends Activity {

  private static final String[] m_CountrIEs={"o型","A型","B型","AB型","其他"};

  private TextView m_txtVIEw;

  private Spinner m_Spinner;

  private ArrayAdapter< String> adapter;

  /** Called when the activity is first created. */

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentVIEw(R.layout.main);

  m_txtView=(TextView)this.findViewById(R.id.TextVIEw01);

  m_Spinner=(Spinner)this.findVIEwById(R.id.Spinner01);

  //將可選內容與ArrayAdapter連接起來

  adapter=new ArrayAdapter< String>(this,android.R.layout.simple_spinner_item,m_CountrIEs);

  //設置下拉列表的風格

  接上頁

  adapter.setDropDownVIEwResource(android.R.layout.simple_spinner_dropdown_item);

  //將adapter 添加到m_Spinner中

  m_Spinner.setAdapter(adapter);

  //添加事件Spinner事件監聽

  m_Spinner.setOnItemSelectedListener(m_SpinnerListener);

  //設置默認值

  m_Spinner.setVisibility(VIEw.VISIBLE);

  }

  private Spinner.OnItemSelectedListener m_SpinnerListener=new Spinner.OnItemSelectedListener()

  {

  public void onItemSelected(AdapterView< ?> arg0, VIEw arg1, int arg2,

  long arg3) {

  // TODO Auto-generated method stub

  m_txtView.setText("你的血型是:"+m_CountrIEs[arg2]);

  //arg0.setVisibility(VIEw.VISIBLE);

  }

  public void onNothingSelected(AdapterVIEw< ?> arg0) {

  // TODO Auto-generated method stub

  }

  };

  }

  第二個demo

  main.XML中

  Java代碼 收藏代碼

  < ?XML version="1.0" encoding="utf-8"?>

  < LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"

  android:orIEntation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  >

  < Spinner android:id="@+id/Spinner01"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content">

  < /Spinner>

  < /LinearLayout>

  再新建一個item.XML

  Java代碼 收藏代碼

  < ?XML version="1.0" encoding="utf-8"?>

  < LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"

  android:orIEntation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  < TextVIEw

  android:id="@+id/TextVIEw01"

  android:layout_width="wrap_content"

  接上頁

  android:layout_height="wrap_content">

  < /TextVIEw>

  < /LinearLayout>

  主代碼中

  Java代碼 收藏代碼

  package com.test;

  import android.app.Activity;

  import android.os.Bundle;

  import android.widget.ArrayAdapter;

  import android.widget.Spinner;

  public class aaa extends Activity{

  private Spinner sex;

  private String sexs[] = {"男","女"};

  private ArrayAdapter< String> adapter = null;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  // TODO Auto-generated method stub

  super.onCreate(savedInstanceState);

  this.setContentVIEw(R.layout.aaa);

  sex=(Spinner)findVIEwById(R.id.Spinner01);

  adapter = new ArrayAdapter< String>(this, R.layout.item, R.id.TextVIEw01, sexs);

  sex.setAdapter(adapter);

  }

  }

  第一個例子和第二個例子的區別在於第二個是用戶自定義的下拉菜單格式

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