Android控件Spinner的使用方法(1)
一、使用方法
1、在layout中創(chuàng)建Spinner控件
<Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" />
2、給數(shù)據(jù)適配器添加數(shù)據(jù)源和顯示格式
String[] city=new String[]{"北京","上海","香港","澳門"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, city);

3、給Spinner控件添加數(shù)據(jù)適配器
spinner1.setAdapter(adapter);
二、效果圖及代碼


fry.Activity01
package fry;
import com.example.SpinnerDemo.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class Activity01 extends Activity{
private Spinner spinner1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
spinner1=(Spinner) findViewById(R.id.spinner1);
initSpinner1();
}
public void initSpinner1(){
String[] city=new String[]{"北京","上海","香港","澳門"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, city);
spinner1.setAdapter(adapter);
}
}
/SpinnerDemo/res/layout/activity01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
三、注意點(diǎn)
1、android.R.layout.simple_spinner_dropdown_item
2、String[] city=new String[]{"北京","上海","香港","澳門"}; 字符串使用
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
- Android中Spinner(下拉框)控件的使用詳解
- android 之Spinner下拉菜單實(shí)現(xiàn)級(jí)聯(lián)
- Android Spinner 下拉菜單的使用
- Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))
- Android實(shí)現(xiàn)下拉菜單Spinner效果
- Android UI組件Spinner下拉列表詳解
- Android中Spinner控件之鍵值對(duì)用法實(shí)例分析
- Android下拉列表spinner的實(shí)例代碼
- android應(yīng)用開發(fā)之spinner控件的簡單使用
相關(guān)文章
Android this與Activity.this的區(qū)別
這篇文章主要介紹了 Android this與Activity.this的區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-09-09
關(guān)于Android Studio封裝SDK的那些事兒
這篇文章主要給大家介紹了關(guān)于Android Studio封裝SDK的那些事兒,文中通過圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
Android退出應(yīng)用最優(yōu)雅的方式(改進(jìn)版)
這篇文章主要介紹了Android退出應(yīng)用最優(yōu)雅的方式,改進(jìn)版,感興趣的小伙伴們可以參考一下2016-01-01
Kotlin 協(xié)程 supervisorScope {} 運(yùn)行崩潰解決方法
看過很多?supervisorScope {}?文檔的使用,我照抄一摸一樣的代碼,運(yùn)行就崩潰,最后找到了解決方法,應(yīng)該是kotlin版本更新做過改動(dòng),當(dāng)前我使用的是?androidx.core:core-ktx:1.9.0,本文給大家介紹Kotlin 協(xié)程 supervisorScope {} 運(yùn)行崩潰解決方法,感興趣的朋友一起看看吧2024-01-01
實(shí)現(xiàn)Android 獲取cache緩存的目錄路徑的方法
這篇文章主要介紹了實(shí)現(xiàn)Android 獲取cache緩存的目錄路徑的方法的相關(guān)資料,這里實(shí)現(xiàn)一個(gè)靜態(tài)類來實(shí)現(xiàn)該功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08
Android實(shí)現(xiàn)Banner界面廣告圖片循環(huán)輪播(包括實(shí)現(xiàn)手動(dòng)滑動(dòng)循環(huán))
這篇文章主要介紹了Android實(shí)現(xiàn)Banner界面廣告圖片循環(huán)輪播(包括實(shí)現(xiàn)手動(dòng)滑動(dòng)循環(huán))的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android雷達(dá)掃描動(dòng)態(tài)界面制作
這篇文章主要為大家詳細(xì)介紹了Android雷達(dá)掃描動(dòng)態(tài)界面制作資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

