android入门 spinner

package com.zte.android.lession;   
  
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author rychenga
 * 
 * 一个spinner、2个button 组件
 * 单击button时完成spinner的数据获取操作
 */
public class Activity_011_Spinner extends Activity
{
	private Button btn1 ;
	private Button btn2 ;
	private Spinner spinner ;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_011_spinner_layout);
		
		//初始化组件
		btn1 = (Button)findViewById(R.id.l011_button1);
		btn2 = (Button)findViewById(R.id.l011_button2);
		spinner = (Spinner)findViewById(R.id.l011_spinner1);
		
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(Activity_011_Spinner.this, 
						String.valueOf(spinner.getCount()), 
						Toast.LENGTH_LONG).show();
			}
		});
		
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(Activity_011_Spinner.this, 
							String.valueOf(spinner.getSelectedItem()),
							Toast.LENGTH_LONG).show();
			}
		});
		
	}
	
}  


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/l011_spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/activity_011_items" />

    <Button
        android:id="@+id/l011_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示选项数" />

    <Button
        android:id="@+id/l011_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示选项" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="activity_011_items">
        <item>选项一</item>
        <item>选项二</item>
        <item>选项三</item>
        <item>选项四</item>
        <item>选项五</item>
        <item>选项六</item>
    </string-array>
</resources>

你可能感兴趣的:(android)