android 之spinner用法

1.聲明Spinner
<Spinner
android:id=”@+id/spinnerId”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>
2.聲明數組

zhangsan
lisi
wangwu
mazi
yuexin
chuxing
xihang
xiongfei

3.創建一個ArrayAdapter(給控件提供數據)

(1)靜態創建

Spinner spinner = (Spinner) findViewById(R.id.spinnerId);
//創建adapter對象,參數1為上下文,參數2為數據的來源,

參數3為下拉菜單的樣式(這裡為系統自帶的樣式)
ArrayAdapter adapter =

ArrayAdapter.createFromResource(this,
R.array.students_array,

android.R.layout.simple_spinner_item);
//佈局文件定義下拉菜單的選項的樣子
adapter.setDropDownViewResource

(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//下拉菜單的題目

spinner.setPrompt(“測試”);

(2)動態創建

List list = new ArrayList();
list.add(“yuexin”);
list.add(“xihang”);
list.add(“xiongfei”);
//參數1為上下文,參數2自定義的item,參數3為參數2佈局中的textview(參數2中必須要有textview用於顯示),參數4為準備顯示的list
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item, R.id.spinner_textView,list);

4.創建監聽

spinner.setOnItemSelectedListener(new spinnerOnItemSelectedListener

());

class spinnerOnItemSelectedListener implements OnItemSelectedListener{

@Override
public void onItemSelected(AdapterView adapterView,

View view, int position,
long id) {
//得到其中的值
String selected =

adapterView.getItemAtPosition(position).toString();
System.out.println(selected);
}

@Override
public void onNothingSelected(AdapterView arg0) {
System.out.println(“nothing”);
}

}

代碼:

MainActivity.java

package com.yx.spinner;

import java.util.ArrayList;
import java.util.List;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinnerId);

//動態加載、、、、、、、、、
List list = new ArrayList();
list.add(“yuexin”);
list.add(“xihang”);
list.add(“xiongfei”);
//參數1為上下文,參數2自定義的item,參數3為參數2佈局中的textview(參數2中必須要有textview用於顯示),參數4為準備顯示的list
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item, R.id.spinner_textView,list);
//動態加載、、、、

/*//靜態加載,從strings.xml中加載
//創建adapter對象為spinner提供數據和樣式,參數1為上下文,參數2為數據的來源(在strings.xml文件中定義的),參數3為下拉菜單的樣式(這裡為系統自帶的樣式)
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.students_array, android.R.layout.simple_spinner_item);
//佈局文件定義下拉菜單的選項的樣子
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
*/
spinner.setAdapter(adapter);
//下拉菜單的題目
spinner.setPrompt(“測試”);

spinner.setOnItemSelectedListener(new spinnerOnItemSelectedListener());
}

class spinnerOnItemSelectedListener implements OnItemSelectedListener{

@Override
public void onItemSelected(AdapterView adapterView, View view, int position,
long id) {
//得到其中的值
String selected = adapterView.getItemAtPosition(position).toString();
System.out.println(selected);
}

@Override
public void onNothingSelected(AdapterView arg0) {
System.out.println(“nothing”);
}

}

}

item.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”horizontal” >

<ImageView
android:id=”@+id/imageView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@android:drawable/btn_dialog” />

<TextView
android:id=”@+id/spinner_textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
/>

main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools=”https://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/spinnerId”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>

發佈留言