Android實(shí)現(xiàn)下拉菜單Spinner效果
Android 中下拉菜單,即如html中的<select>,關(guān)鍵在于調(diào)用setDropDownViewResource方法,以XML的方式定義下拉菜單要顯示的模樣
1.1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="com.rj141.sb.kongjian.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:text="請選擇您最喜歡的水果:" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:id="@+id/tv" />
</LinearLayout>
Spinner是下拉列表的組件
1.2.MainActivity.class
public class MainActivity extends AppCompatActivity {
private Spinner s;
String[] data=new String[]{"蘋果","雪梨","西瓜","葡萄","橙子","草莓"};
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) this.findViewById(R.id.tv);
s= (Spinner) this.findViewById(R.id.spinner);
s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data));
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String str=data[position];
tv.setText("最喜歡的水果是:"+str);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data));android.R.layout.simple_list_item_1是指安卓自帶的下拉列表格式,data是數(shù)據(jù)源;
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()){..};是下拉列表的監(jiān)聽
效果圖:

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)掌握Android實(shí)現(xiàn)下拉菜單Spinner組件有所幫助。
相關(guān)文章
Android自定義View實(shí)現(xiàn)柱狀波形圖的繪制
柱狀波形圖是一種常見的圖形。一個個柱子按順序排列,構(gòu)成一個波形圖。本文將利用Android自定義View實(shí)現(xiàn)柱狀波形圖的繪制,需要的可以參考一下2022-08-08
Android ViewPager導(dǎo)航小圓點(diǎn)實(shí)現(xiàn)無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager導(dǎo)航小圓點(diǎn)實(shí)現(xiàn)無限循環(huán)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
我們在做項(xiàng)目的時候會用到圓形的圖片,比如用戶頭像,類似QQ。用戶在用QQ更換頭像的時候,上傳的圖片都是矩形的,但顯示的時候確是圓形的。那么這是如何實(shí)現(xiàn)的呢,下面我們就來探討下吧。2015-05-05
Android 6.0動態(tài)權(quán)限申請教程
本文主要介紹了Android 6.0動態(tài)權(quán)限申請的教程,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
講解Android中的Widget及AppWidget小工具的創(chuàng)建實(shí)例
這篇文章主要介紹了講解Android中的Widget及Widget的創(chuàng)建實(shí)例,文中的例子展示了通過RemoteView來溝通AppWidgetProvider與AppWidgetHostView的方法,需要的朋友可以參考下2016-03-03
Android?studio開發(fā)實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android?studio開發(fā)實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05

