Spinner อันแรกใช้เลือกบริษัท และ Spinner อันที่สองใช้เลือกมือถือของบริษัทที่ถูกเลือกจาก Spinner อันแรก
build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.jaredrummler:material-spinner:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="กรุณาเลือกผู้ผลิตและชนิดของสินค้า" android:textColor="#0a0" android:textSize="24sp" /> <com.jaredrummler.materialspinner.MaterialSpinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" app:ms_arrow_tint="#0a0" app:ms_background_color="#fff" app:ms_text_color="#00a" /> <com.jaredrummler.materialspinner.MaterialSpinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" app:ms_arrow_tint="#0a0" app:ms_background_color="#fff" app:ms_text_color="#00a" /> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.phaisarn.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.jaredrummler.materialspinner.MaterialSpinner; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final MaterialSpinner spinner1 = findViewById(R.id.spinner1); String[] items = {"Samsung", "Apple", "Microsoft"}; spinner1.setItems(items); final MaterialSpinner spinner2 = findViewById(R.id.spinner2); setSpinner2(spinner2, spinner1.getText().toString()); spinner1.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() { @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) { setSpinner2(spinner2, item); } }); } private void setSpinner2(MaterialSpinner spinner2, String item) { if (item.equals("Samsung")) { spinner2.setItems("Galaxy S", "Galaxy Note", "Galaxy Tab"); } else if (item.equals("Apple")) { spinner2.setItems("iPhone", "iPad", "MacBook"); } else if (item.equals("Microsoft")) { spinner2.setItems("Surface"); } } } |