2016年7月23日 星期六

SpinnerDemo




main.xml


<LinearLayout 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"    android:orientation="vertical">

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="@string/text_tvFood" />

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

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="@string/text_tvPlace" />

    <Spinner        android:id="@+id/spPlace"        android:layout_width="match_parent"        android:layout_height="wrap_content" />

    <TextView        android:id="@+id/tvMessage"        android:layout_width="match_parent"        android:layout_height="60dp"        android:layout_marginTop="30dp"        android:background="#00FFFF"        android:padding="10dp"        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>


==========================================================================

main.java

==========================================================================


public class MainActivity extends AppCompatActivity {

    private TextView tvMessage;

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

    private void findViews() {
        tvMessage = (TextView) findViewById(R.id.tvMessage);
        Spinner spFood = (Spinner) findViewById(R.id.spFood);
        spFood.setSelection(0, true);

        // 內容已經在這裡指定了 android:entries="@array/food_array"

        // 註冊 setOnItemSelectedListener監聽器....當使用者改變選項會呼叫onItemSelected()
spFood.setOnItemSelectedListener(listener); Spinner spPlace = (Spinner) findViewById(R.id.spPlace); String[] places = {"Australia", "U.K.", "Japan", "Thailand"};
         // 呼叫ArrayApapter 建構式以建立選項的內容與樣式...選項的內容來自於places 字串陣列;  
         //  樣式則套用系統內建的android.R.layout.simple_spinner_item

        ArrayAdapter<String> adapterPlace = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_item, places);
        
       
        adapterPlace.setDropDownViewResource(
                   android.R.layout.simple_spinner_dropdown_item);
        spPlace.setAdapter(adapterPlace);
        spPlace.setSelection(0, true);
        spPlace.setOnItemSelectedListener(listener);
    }

    Spinner.OnItemSelectedListener listener = new Spinner.OnItemSelectedListener() {
        @Override        public void onItemSelected(
                AdapterView<?> parent, View view, int pos, long id) {
            tvMessage.setText(parent.getItemAtPosition(pos).toString());
        }

        @Override        public void onNothingSelected(AdapterView<?> parent) {
            tvMessage.setText("Nothing selected!");
        }
    };
}


沒有留言:

張貼留言