Android 之 下拉框(Spinner)的使用

下拉列表 Spinner。
Spinner的使用,能夠極大提升用戶的體驗性。當須要用戶選擇的時候,能夠提供一個下拉列表將全部可選的項列出來。供用戶選擇。
Demo以下,能夠留做參考 1、使用數組做爲數據源
1.新建一個android的工程 2.工程的layout.xml文件以下:聲明一個TextView控件和一個Spinner控件java

Xml代碼 複製代碼  收藏代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  >
  <TextView android:id="@+id/spinnerText"
  			android:layout_width="fill_parent" 
  			android:layout_height="wrap_content"></TextView>
  <Spinner android:id="@+id/Spinner01" 
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"></Spinner>
</LinearLayout>

3.java代碼android

 
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 static final String[] m={"A型","B型","O型","AB型","其餘"};
	private TextView view ;
	private Spinner spinner;
	private ArrayAdapter<String> adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinner);
		
		view = (TextView) findViewById(R.id.spinnerText);
		spinner = (Spinner) findViewById(R.id.Spinner01);
		//將可選內容與ArrayAdapter鏈接起來
		adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);
		
		//設置下拉列表的風格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		//將adapter 添加到spinner中
		spinner.setAdapter(adapter);
		
		//添加事件Spinner事件監聽  
		spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
		
		//設置默認值
		spinner.setVisibility(View.VISIBLE);
		
	}
	
	//使用數組形式操做
	class SpinnerSelectedListener implements OnItemSelectedListener{

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			view.setText("你的血型是:"+m[arg2]);
		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}
}

四、運行效果以下:



2、使用XML做爲數據源 1.新建一個android的工程
2.在values文件夾下新建一個arryas.xml文件:聲明一個TextView控件和一個Spinner控件,代碼以下:數組

 
<?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>
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) {
			
		}
		
	}
}

四、運行效果以下:

 app

相關文章
相關標籤/搜索