Android UI控件Spinner控件的学习

package com.android.liu.ui;

import com.android.liu.R;

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

public class SpinnerActivity extends Activity {
 private static final String[] m_Countries={"A","B","AB","O","其它"};
 
 private TextView m_TextView;
 private Spinner m_Spinner;
 private ArrayAdapter<String> adapter;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.spinner);
  
  m_TextView=(TextView) this.findViewById(R.id.SpinnerText);
  m_Spinner=(Spinner) this.findViewById(R.id.spinner);
  
  //将可选内容与ArrayAdapter连接
  adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m_Countries);
  //设置列表的下拉风格
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
  m_Spinner.setAdapter(adapter);
  
  m_Spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1,
     int arg2, long arg3) {
    
    m_TextView.setText("你的血型是 :"+m_Countries[arg2]);
    
    //显示当前选中的项
    arg0.setVisibility(View.VISIBLE);
    
   }

   @Override
   public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    
   }});
 }

}

 

<?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/SpinnerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="请选择正确答案" 
    ></TextView>
   
   <Spinner
   android:id="@+id/spinner"
   android:layout_height="wrap_content"
   android:layout_width= "wrap_content" 
   android:layout_centerHorizontal="true"
   ></Spinner>
      
      
      
</LinearLayout>

你可能感兴趣的:(android ui)