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="請(qǐng)選擇您最喜歡的水果:" /> <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[]{"蘋(píng)果","雪梨","西瓜","葡萄","橙子","草莓"}; 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)聽(tīng)
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)掌握Android實(shí)現(xiàn)下拉菜單Spinner組件有所幫助。
相關(guān)文章
Android List刪除重復(fù)數(shù)據(jù)
這篇文章主要介紹了Android List刪除重復(fù)數(shù)據(jù)的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06Android 實(shí)現(xiàn)夜間模式的快速簡(jiǎn)單方法實(shí)例詳解
這篇文章主要介紹了Android 實(shí)現(xiàn)夜間模式的快速簡(jiǎn)單方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法實(shí)例
相信大家應(yīng)該也都發(fā)現(xiàn)了,現(xiàn)在很多應(yīng)用都使用到懸浮窗,例如微信在視頻的時(shí)候,點(diǎn)擊Home鍵,視頻小窗口仍然會(huì)在屏幕上顯示,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)懸浮窗的簡(jiǎn)單方法,需要的朋友可以參考下2021-09-09Android View源碼解讀 DecorView與ViewRootImpl淺談
這篇文章主要解讀了Android View源碼,為大家詳細(xì)介紹DecorView與ViewRootImpl,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02一文詳解Android無(wú)需權(quán)限調(diào)用系統(tǒng)相機(jī)拍照
這篇文章主要為大家介紹了Android無(wú)需權(quán)限調(diào)用系統(tǒng)相機(jī)拍照詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03