愛一個人不定要擁有,寫一篇鐵人文不需要很久。你加油!
大家好今天我要來介紹一個簡單的元件---spinner,也就是常說的下拉式選單,顧名思義下拉式選單就是當我點擊到他時,他會向下彈出一些資料來讓使用者選取,廢話不多說那我就開始示範如何使用下拉式選單吧!
一樣一開始先上程式碼
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請選擇要選的地方"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
在主畫面佈局方面我只給他一個簡單的TextView和一個待會會使用到的spinner。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/image1"
android:layout_width="40dp"
android:layout_height="40dp"
/>
<TextView
android:id="@+id/continent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
在這我給他一個ImageView和一個TextView,待會會將資料塞入
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.Map;
public class ExampleAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
List<Map<String,Object>> listItem;
public ExampleAdapter(Context context, List<Map<String, Object>> listItem) {
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listItem = listItem;
}
@Override
public int getCount() {
return listItem.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.example_continent,parent,false);
ImageView imageView = view.findViewById(R.id.image1);
TextView textView = view.findViewById(R.id.continent);
imageView.setImageResource(Integer.valueOf(listItem.get(position).get("continent_image").toString()));
textView.setText(listItem.get(position).get("continent").toString());
return view;
}
}
在這邊使用到了前幾天示範給各位的BaseAdapter
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
Spinner mSpinner;
String [] continent = {"北美洲","南美洲","亞洲","歐洲","非洲","大洋洲","南極洲"};
int[] continent_image = {R.drawable.north_america,R.drawable.south_america,R.drawable.asia,R.drawable.europe
,R.drawable.africa,R.drawable.oceania,R.drawable.antarctica
};
ExampleAdapter exampleAdapter;
List<Map<String,Object>> areas = new ArrayList<Map<String,Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = findViewById(R.id.spinner);
for (int i = 0; i< continent_image.length; i++){
Map<String,Object> area = new HashMap<String, Object>();
area.put("continent_image", continent_image[i]);
area.put("continent",continent[i]);
areas.add(area);
}
exampleAdapter = new ExampleAdapter(MainActivity.this,areas);
mSpinner.setAdapter(exampleAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"您點選的是:"+ continent[position],Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
在MainActivity裡面一樣使用到了七大洲的圖片和名字,程式碼和前幾天示範的ListView3差不多,大概有新增的就是新宣告了一個Spinner並且findViewByID找到元件,然後setAdapter和setOnItemSelectedListener,setOnItemSelectedListener是當我們點擊spinner後要做的動作是什麼,onItemSelected是選項有選取時的動作,onNothingSelected則是選項沒有選取時的動作。
那今天的示範就到這了,謝謝大家的觀看。