<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/color"
        android:id="@+id/textView" />

    <Spinner
        android:id="@+id/color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="@string/hello"
        android:layout_below="@+id/color"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>




<resources>
    <string name="app_name">Spinner</string>
    <string name="action_settings">Settings</string>
    <string name="color">Color</string>
    <string name="hello">Hello World!!</string>
    <string-array name="colors">
        <item>BLUE</item>
        <item>GREEN</item>
        <item>YELLOW</item>
        <item>RED</item>
        <item>BLACK</item>
    </string-array>
</resources>




package com.lcpan.spinner;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private Spinner color;
    private TextView output;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        color = (Spinner) findViewById(R.id.color);
        output = (TextView) findViewById(R.id.output);
        color.setOnItemSelectedListener(this);

        String[] colors = getResources().getStringArray(R.array.colors);
        //(R.array.colors)是string.xml中設定的<string-array name="colors"><item></item></Spinner>
        //getResources()是去看R.java檔中的資料,.getStringArray代表尋找string-array的資料
        ArrayAdapter<String> color_adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, colors);
        //ArrayAdapter<String>(MainActivity物件,想要的下拉式選單樣式, 選單內容陣列)
// ArrayAdapter(Context context, int resource, T[] objects) 
   /* ArrayAdapter<CharSequence> color_adapter = ArrayAdapter.createFromResource(this, R.array.colors,
                  android.R.layout.simple_spinner_item);
   */
        color_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
/ Sets the layout resource to create the drop down views. 
        color.setAdapter(color_adapter);         
          Spinner.setAdapter(SpinnerAdapter adapter)
          Sets the SpinnerAdapter used to provide the data which backs this Spinner.
 
    }

    public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
        String itemColor = (String) parent.getSelectedItem();
        if (itemColor.equals("BLUE"))
            output.setTextColor(Color.BLUE);
        else if (itemColor.equals("GREEN"))
            output.setTextColor(Color.GREEN);
        else if (itemColor.equals("YELLOW"))
            output.setTextColor(Color.YELLOW);
        else if (itemColor.equals("RED"))
            output.setTextColor(Color.RED);
        else if (itemColor.equals("BLACK"))
            output.setTextColor(Color.BLACK);
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 乙方 的頭像
    乙方

    學習筆記專區

    乙方 發表在 痞客邦 留言(0) 人氣()