วันศุกร์ที่ 20 กรกฎาคม พ.ศ. 2555

Android Spinner

-->
 Android Spinner Example Code

SpinnerActivity.java Code
package android.example.spinner;

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;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class SpinnerActivity extends Activity {
     
      TextView tvSpinner1,tvSpinner2;
     
      void showToast(CharSequence msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_1);
       
        tvSpinner1 = (TextView) findViewById(R.id.textViewSpinner1);
        tvSpinner2 = (TextView) findViewById(R.id.textViewSpinner2);
       
        Spinner s1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.colors, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {
                        showToast("Spinner1: position=" + position + " id=" + id);
                        tvSpinner1.setText(" Position=" + position + " id=" + id);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                        showToast("Spinner1: unselected");
                    }
                });

        Spinner s2 = (Spinner) findViewById(R.id.spinner2);
        adapter = ArrayAdapter.createFromResource(this, R.array.planets,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
        s2.setAdapter(adapter);
        s2.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {
                        showToast("Spinner2: position=" + position + " id=" + id);
                        tvSpinner2.setText(" Position=" + position + " id=" + id);
                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                        showToast("Spinner2: unselected");
                    }
                });
    }
}



spinner.xml Code

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:background="#660287"
    android:layout_width="match_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22dp"
        android:textStyle="bold"
        android:text="@string/spinner_1_color"/>

    <Spinner android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"      
        android:prompt="@string/spinner_1_color_prompt"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:textStyle="bold"
        android:textSize="22dp"
        android:text="@string/spinner_1_planet"/>

    <Spinner android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:prompt="@string/spinner_1_planet_prompt"/>

    <TextView      
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Spinner 1 Select :"
       
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewSpinner1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#f7f887"
        android:textColor="#0000FF"
        android:text=" "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
       
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Spinner 2 Select :"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewSpinner2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#0000FF"
        android:background="#f7f887"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />
   
</LinearLayout>




Download Android Spinner Example Code 



Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android

ติดต่อ amphancm@gmail.com

-->