欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android UI組件Spinner下拉列表詳解

 更新時(shí)間:2021年09月23日 11:29:39   作者:qq_27630169  
這篇文章主要為大家詳細(xì)介紹了Android UI組件Spinner下拉列表的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Spinner下拉列表的實(shí)現(xiàn)代碼,分享給大家

該布局對應(yīng)的關(guān)系圖:

常用屬性:android:entries(指定spinner要顯示的字符串資源。必須是在strings資源文件中定義的字符串資源)android:spinnerMode(spinner的模式,枚舉值有兩個值dialog彈窗顯示和dropdown下拉顯示)android:dropDownWidth(下拉框的寬度,單位通常是dp)android:prompt(當(dāng)spinnerMode的值是dialog時(shí),彈出的對話框式的下列列表的提示。如果
spinnerMode的值是dropdown時(shí)沒有效果。注意:此處的值不能直接使用直接字符串,
必須使用引用(字符串資源))

1.通過entries設(shè)置數(shù)據(jù)項(xiàng),在values文件夾下的strings中添加數(shù)據(jù)的值

在strings.xml中添加一組array數(shù)據(jù)項(xiàng),然后通過在entries中設(shè)置就可以設(shè)置對應(yīng)的值

<Spinner
    android:layout_width="match_parent"
    android:entries="@array/data"http://資源文件設(shè)置數(shù)據(jù)
    android:layout_height="wrap_content">
 </Spinner>

2.設(shè)置android:spinnerMode:

<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:entries="@array/data"
    android:spinnerMode="dialog"
    android:layout_height="wrap_content">
  </Spinner>

值為dialog的顯示為彈框顯示

值如果為dropdown的顯示如下:

android:dropDownWidth設(shè)置下拉寬度

<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:entries="@array/data"
    android:spinnerMode="dropdown"
    android:dropDownWidth="70dp"
    android:layout_height="wrap_content">
  </Spinner>

效果如下圖:

數(shù)據(jù)源的獲取方式:通過ArrayAdapter適配器設(shè)置數(shù)據(jù)數(shù)據(jù)>

什么是適配器:將控件在加載數(shù)據(jù)過程中的同樣的部分 抽取為代碼,每次加載的時(shí)候都調(diào)用這部分代碼,生成
要返回的內(nèi)容,類似于模具
關(guān)于ArrayAdapter簡單介紹下:
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,data);

關(guān)于ArrayAdapter構(gòu)造方法的說明:

1、ArrayAdapter(context, resource, objects)
參數(shù)一:上下文對象
參數(shù)二:布局文件的id,注意該布局文件有且只能有一個TextView標(biāo)簽
參數(shù)三:原始數(shù)據(jù),List集合或數(shù)組都可以。
2、ArrayAdapter(context, resource, textViewResourceId, objects)
參數(shù)一:上下文對象
參數(shù)二:布局文件的id,注意該布局文件中至少有一個TextView標(biāo)簽
參數(shù)三:參數(shù)二布局文件中要顯示數(shù)據(jù)的TextView的id
參數(shù)四:原始數(shù)據(jù),List集合或數(shù)組都可以。

public class MainActivity extends AppCompatActivity {
  private String[] data;
  private List<String> data1;
  private Spinner spinner;
  private ArrayAdapter<String> adapter;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spinner);
    spinner = (Spinner)findViewById(R.id.spinner);
    data = getResources().getStringArray(R.array.data);
    data1 = new ArrayList<>();
    for(int i = 1; i < 10; i++){
      data1.add("這是第" + i +"個");
    }
    //data可以修改為data1,數(shù)據(jù)可以是數(shù)組也可以是集合
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,data);
    spinner.setAdapter(adapter);
  }
}

監(jiān)聽事件

對于Spinner使用的監(jiān)聽事件為:setOnItemSelectedListener(OnItemSelectedListener listener)

public class SpinnerActivity extends Activity implements OnItemSelectedListener {

  /**
  * 當(dāng)item被選中時(shí),會調(diào)用此方法
  */
  public void onItemSelected(AdapterView<?> parent, View view, 
      int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
  }
  /**
  * 當(dāng)數(shù)據(jù)項(xiàng)的值設(shè)置為空時(shí),就會調(diào)用此方法,通過調(diào)用adapter.clear()方法清空數(shù)據(jù),并且刷新界面
  * 時(shí),會調(diào)用次方法
  */
  public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android高仿微信聊天界面代碼分享

    Android高仿微信聊天界面代碼分享

    微信聊天現(xiàn)在非?;?,是因其界面漂亮嗎,哈哈,也許吧。微信每條消息都帶有一個氣泡,非常迷人,看起來感覺實(shí)現(xiàn)起來非常難,其實(shí)并不難。下面小編給大家分享實(shí)現(xiàn)代碼
    2016-02-02
  • Android編程簡易實(shí)現(xiàn)XML解析的方法詳解

    Android編程簡易實(shí)現(xiàn)XML解析的方法詳解

    這篇文章主要介紹了Android編程簡易實(shí)現(xiàn)XML解析的方法,結(jié)合實(shí)例形式總結(jié)分析了Android操作xml文件的各種常見技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-08-08
  • Android仿微信群聊頭像效果

    Android仿微信群聊頭像效果

    這篇文章主要為大家詳細(xì)介紹了Android仿微信群聊頭像效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android實(shí)現(xiàn)多媒體之播放音樂

    Android實(shí)現(xiàn)多媒體之播放音樂

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多媒體之播放音樂的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android IPC機(jī)制綁定Service實(shí)現(xiàn)本地通信

    Android IPC機(jī)制綁定Service實(shí)現(xiàn)本地通信

    本文主要介紹Android IPC機(jī)制綁定Service 實(shí)現(xiàn)本地通信,通過圖解,代碼等方式給大家解釋Android IPC機(jī)制,需要參考的同學(xué)可以看一下
    2016-07-07
  • Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法

    Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法

    這篇文章主要介紹了Android中ListView設(shè)置靜態(tài)數(shù)據(jù)的方法,如何為ListView設(shè)置靜態(tài)數(shù)據(jù),感興趣的小伙伴們可以參考一下
    2015-12-12
  • AndroidStudio修改Code Style來格式化自定義標(biāo)簽的xml文件方式

    AndroidStudio修改Code Style來格式化自定義標(biāo)簽的xml文件方式

    這篇文章主要介紹了AndroidStudio修改Code Style來格式化自定義標(biāo)簽的xml文件方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android通過Java sdk的方式接入OpenCv的方法

    Android通過Java sdk的方式接入OpenCv的方法

    這篇文章主要介紹了Android通過Java sdk的方式接入OpenCv的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Android入門之計(jì)時(shí)器Chronometer的使用教程

    Android入門之計(jì)時(shí)器Chronometer的使用教程

    Chronometer是一個簡單的定時(shí)器,你可以給它一個開始時(shí)間,并以此定時(shí)。本文將利用個簡單的示例為大家講解一下它的使用,感興趣的小伙伴可以嘗試一下
    2022-11-11
  • Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路

    Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路

    這篇文章主要介紹了Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路的相關(guān)資料,在開發(fā)網(wǎng)路狀態(tài)的時(shí)候需要先判斷是否開啟之后在提示用戶進(jìn)行開啟操作,需要的朋友可以參考下
    2017-08-08

最新評論