本次要介紹的原件為Spinner,
Spinner為下拉式選單,
其優點為節省空間,
本篇會先說明如何建置Spinner及選項,
再講解觸發事件如何撰寫
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="欲重修科目"
android:textSize="25sp"
android:layout_marginLeft="5dp"/>
<Spinner android:id="@+id/course"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_margin="5dp"
android:background="#E6E6E6"/>
<TextView android:id="@+id/tx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
版面的部份使用LinearLayout建立兩個TextView及一個Spinner,
而Spinner下拉選項的部分必須至java進行撰寫
package com.example.spinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {
private Spinner course;
// 1
String[] Subject = new String[] {"英文","國文","數學"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
course = findViewById(R.id.course);
// 2
ArrayAdapter<String> adapterSubject = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,Subject);
// 3
adapterSubject.setDropDownViewResource(android.R.layout.simple_spinner_item);
// 4
course.setAdapter(adapterSubject);
}
}
執行後的畫面
點擊Spinner會出現其他建立的選項
以上的程式無任何功能,因此我們要加入監聽事件,並完成簡單的動作
package com.example.spinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
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 {
private Spinner course;
private TextView tx;
String[] Subject = new String[]{"英文", "國文", "數學"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
course = findViewById(R.id.course);
tx = findViewById(R.id.tx);
ArrayAdapter<String> adapterSubject = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Subject);
adapterSubject.setDropDownViewResource(android.R.layout.simple_spinner_item);
course.setAdapter(adapterSubject);
course.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String show = parent.getSelectedItem().toString();
tx.setText("您所選的科目為" +show);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
加入監聽事件後,在執行的地方寫入String show = parent.getSelectedItem().toString();
意思是將Spinner所選的選項轉換為String
並於下行setText的地方使其顯示於TextView
以下是執行畫面