怎么在Android中使用Spinner控件實(shí)現(xiàn)一個(gè)下拉框功能-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)怎么在Android中使用 Spinner控件實(shí)現(xiàn)一個(gè)下拉框功能,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比清流網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式清流網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋清流地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。Android是什么

Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。

Spinner是android的一種控件,用它我們可以實(shí)現(xiàn)下拉框。

我們先來(lái)看一下效果圖

怎么在Android中使用 Spinner控件實(shí)現(xiàn)一個(gè)下拉框功能

怎么在Android中使用 Spinner控件實(shí)現(xiàn)一個(gè)下拉框功能

這是一個(gè)很簡(jiǎn)單的功能,上面一個(gè)TextView,下面一個(gè)Spinner,TextView用于顯示Spinner選擇的選項(xiàng)。

下面我們就來(lái)看一下實(shí)現(xiàn)吧。

首先,我們先在xml文件中將spinner寫(xiě)出

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
 android:orientation="vertical"
 tools:context=".MainActivity">
 <TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/spinner_textview"/>
 <Spinner
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/spinner1"></Spinner>
</LinearLayout>

類似于ListView,Spinner也需要一個(gè)List和一個(gè)Adapter來(lái)為其提供顯示的數(shù)據(jù)。

public class MainActivity extends AppCompatActivity {
 private List<String> teamList;
 private TextView textView;
 private Spinner spinner1;
 private ArrayAdapter<String> arrayAdapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initView();
  //設(shè)置下拉列表的風(fēng)格
  arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  //將adapter 添加到spinner中
  spinner1.setAdapter(arrayAdapter);
  //設(shè)置點(diǎn)擊事件
  spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
   @Override
   public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    textView.setText(teamList.get(i));
   }
   @Override
   public void onNothingSelected(AdapterView<?> adapterView) {
   }
  });
 }
 public void initView(){
  teamList = new ArrayList<>();
  initList();
  textView = findViewById(R.id.spinner_textview);
  spinner1 = findViewById(R.id.spinner1);
  arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,teamList);
 }
 public void initList(){
  teamList.add("羅馬");
  teamList.add("那不勒斯");
  teamList.add("國(guó)際米蘭");
  teamList.add("AC米蘭");
 }
}

源碼地址

下面單獨(dú)看下Spinner的功能和用法

Spinner其實(shí)是一個(gè)列表選擇框,不過(guò)Android的列表選擇框并不需要顯示下拉列表,而是相當(dāng)于彈出一個(gè)菜單供用戶選擇。

Spinner與Gallery都繼承了AbsSpinner,AbsSpinner繼承了AdapterView,因此他也表現(xiàn)出AdapterView的特征:只要為AdapterView提供Adapter即可。

android:entries屬性并不是Spinner定義的,而不是AbsSpinner中定義的,因此Gallery(繼承了AbsSpinner)也支持該XML屬性。

如果開(kāi)發(fā)者使用Spinner時(shí)已經(jīng)可以確定列表選擇框里的列表項(xiàng),則完全不需要編寫(xiě)代碼,只要為Spinner指定android:entries屬性即可讓Spinner正常工作;如果程序需要在程序運(yùn)行時(shí)動(dòng)態(tài)決定Spinner的列表項(xiàng),或者程序需要對(duì)Spinner的列表項(xiàng)進(jìn)行定制,則可使用Adapter提供列表項(xiàng)。

如下界面布局文件中定義了兩個(gè)Spinner組件,其中一個(gè)Spinner組件指定了android:entries屬性,因此需要在Activity中為他設(shè)置Adapter。

<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">
 <!--定義一個(gè)Spinner組件,指定顯示該Spinner組件的數(shù)組-->
 <Spinner
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:entries="@array/books"
  android:popupBackground="#66ccff"
  android:dropDownWidth="230dp"
  ></Spinner>
 <Spinner
  android:id="@+id/spinner"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  ></Spinner>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
 Spinner spinner;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //獲取界面布局文件的Spinner組件
  spinner= (Spinner) findViewById(R.id.spinner);
  String[] arr={"孫悟空","豬八戒","唐僧"};
  //創(chuàng)建ArrayAdapter對(duì)象
  ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,arr);
  spinner.setAdapter(adapter);
 }
}

以上就是怎么在Android中使用 Spinner控件實(shí)現(xiàn)一個(gè)下拉框功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享文章:怎么在Android中使用Spinner控件實(shí)現(xiàn)一個(gè)下拉框功能-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)路徑:http://muchs.cn/article46/cosghg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、標(biāo)簽優(yōu)化網(wǎng)站收錄、域名注冊(cè)小程序開(kāi)發(fā)、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作