總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年1月13日 星期五

How to use Spinner setOnItemSelectedListener








下拉選單Spinner讓Android手機在有限的畫面範圍內仍可放入所需的資料, 並可以實作OnItemSelectedListener增加使用者的點選事件.

在本範例中放置了兩個Spinner Widget,並且依user的選擇不同而有對應的文字顯示在TextView中



Spinner_getValue.java
package com.tsots.Spinner_getValue;

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

public class Spinner_getValue extends Activity 
{
 Context context = Spinner_getValue.this;
 ImageView background;
 Spinner spinner1;
 Spinner spinner2;
 TextView textview1;
 TextView textview2;
 TextView textview3;
 TextView textview4;
 TextView textview_method;
 TextView textview_material;
 TextView textview_spinner1_step;
 TextView textview_spinner2_step;
 String spinner1_position = "0";
 String spinner2_position = "0";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_getvalue);
        
        //設背景圖&透明度
        background = (ImageView) findViewById(R.id.imageview_backgroud);
        background.setImageDrawable(getResources().getDrawable(R.drawable.logo));
        background.setAlpha(100);

        spinner1 = (Spinner) findViewById (R.id.spinner1);
        spinner2 = (Spinner) findViewById (R.id.spinner2);
        textview1 = (TextView) findViewById (R.id.textview1);
        textview2 = (TextView) findViewById (R.id.textview2);
        textview3 = (TextView) findViewById (R.id.textview3);
        textview4 = (TextView) findViewById (R.id.textview4);
        textview_material = (TextView) findViewById (R.id.textview_material);
        textview_method = (TextView) findViewById (R.id.textview_method);
        textview_spinner2_step = (TextView) findViewById (R.id.textview_spinner2_step);
        textview_spinner1_step = (TextView) findViewById (R.id.textview_spinner1_step);
 
        /*
         * 必須為Spinner設定Adapter(外觀樣式及傳入的資料), 在此我們先用Android的default值
         * android.R.layout.simple_spinner_item
         * android.R.layout.simple_spinner_dropdown_item
         */
        ArrayAdapter adapter_spinner1 = new ArrayAdapter(context, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.array_spinner_material));
        adapter_spinner1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter_spinner1);
        //定義Spinner選單select item後的行為
        spinner1.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {
         public void onItemSelected(AdapterView adapterView, View view, int position, long id)
         {
          //int position: 會傳入user選到的item的位置
          //int id: 會傳入user選到的item的idtextview1.setText(adapterView.getSelectedItem().toString());
          textview2.setText(String.valueOf(position));
          spinner1_position = String.valueOf(position);          
                
                if(spinner1_position.equals("0"))
                {
                 textview_method.setText(
                       getResources().getString(R.string.str_chemistry_method3)+"\n"+
                       getResources().getString(R.string.str_chemistry_method4)
                         ); 
                 textview_spinner1_step.setText(getResources().getString(R.string.str_chemistry_alcohol_metal_step));
                }
                else if(spinner1_position.equals("1"))
                {
                 textview_method.setText(
                       getResources().getString(R.string.str_chemistry_method1)+"\n"+
                       getResources().getString(R.string.str_chemistry_method2)+"\n"+
                       getResources().getString(R.string.str_chemistry_method3)+"\n"+
                       getResources().getString(R.string.str_chemistry_method4)
                         );
                 textview_spinner1_step.setText(getResources().getString(R.string.str_chemistry_material2_step));
                }    
                else if(spinner1_position.equals("2"))
                {
                 textview_method.setText(
           getResources().getString(R.string.str_chemistry_method3)+"\n"+
           getResources().getString(R.string.str_chemistry_method4)
                );   
                 textview_spinner1_step.setText(getResources().getString(R.string.str_chemistry_material3_step));
                }
                else if(spinner1_position.equals("3"))
                {
                 textview_method.setText(getResources().getString(R.string.str_chemistry_method3)); 
                 textview_spinner1_step.setText(getResources().getString(R.string.str_chemistry_material4_step));
                }
                else
                {                 
                 textview_method.setText(
           getResources().getString(R.string.str_chemistry_method1)+"\n"+
           getResources().getString(R.string.str_chemistry_method2)
                   ); 
                 textview_spinner1_step.setText(getResources().getString(R.string.str_chemistry_material5_step));
                }
         }

   public void onNothingSelected(AdapterView arg0) 
   {
    // TODO Auto-generated method stub    
   }
        });

        /*
         * 必須為Spinner設定Adapter(外觀樣式及傳入的資料), 在此我們先用Android的default值
         * android.R.layout.simple_spinner_item
         * android.R.layout.simple_spinner_dropdown_item
         */
        ArrayAdapter adapter_spinner2 = null;  
        adapter_spinner2 = new ArrayAdapter(context, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.array_spinner2_style2));
        adapter_spinner2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter_spinner2);
        //定義Spinner選單select item後的行為
        spinner2.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
        {
         public void onItemSelected(AdapterView adapterView, View view, int position, long id)
         {
          //int position: 會傳入user選到的item的位置
          //int id: 會傳入user選到的item的idtextview3.setText(adapterView.getSelectedItem().toString());
          textview4.setText(String.valueOf(position));
          spinner2_position = textview4.getText().toString();
          if(spinner2_position.equals("0"))
          {
           textview_material.setText(getResources().getString(R.string.str_prompt_material1));       
           textview_spinner2_step.setText(getResources().getString(R.string.str_spinner2_method1));
          }
          else if(spinner2_position.equals("1"))
          {
           textview_material.setText(getResources().getString(R.string.str_prompt_material2));
           textview_spinner2_step.setText(getResources().getString(R.string.str_spinner2_method2));
          }
          else if(spinner2_position.equals("2"))
          {
           textview_material.setText(getResources().getString(R.string.str_prompt_material1));
           textview_spinner2_step.setText(getResources().getString(R.string.str_spinner2_method3));
          }
         }
   public void onNothingSelected(AdapterView arg0) 
   {
    // TODO Auto-generated method stub    
   }
        });
    }
}//

沒有留言:

張貼留言