- package com.example.student.myapplication;
- import android.support.v7.app.AppCompatActivity;
- 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.Toast;
- public class MainActivity extends AppCompatActivity {
- String menu[] = {"羽球","籃球","排球","游泳","乒乓球"};
- MyAdapater arrayAdapter;
- Spinner sp;
- ImageView image[] = ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- sp = (Spinner) findViewById(R.id.spinner);
- arrayAdapter = new MyAdapater(this,R.layout.support_simple_spinner_dropdown_item,menu,image);
- sp.setAdapter(arrayAdapter);
- sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- Toast.makeText(MainActivity.this, String.format("你喜歡%s", menu[position]),Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- }
- }
複製代碼- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageView"
- android:layout_alignParentTop="true"
- android:layout_alignParentStart="true" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="烤雞"
- android:id="@+id/textView2"
- android:layout_below="@+id/imageView"
- android:layout_toEndOf="@+id/imageView"
- android:layout_marginStart="34dp" />
- </RelativeLayout>
複製代碼 |