亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用 Spinner控件實(shí)現(xiàn)下拉框功能

 更新時(shí)間:2018年08月21日 15:21:40   作者:小法老沙拉維  
Spinner是android的一種控件,用它我們可以實(shí)現(xiàn)下拉框。下面通過實(shí)例代碼給大家介紹Android使用 Spinner控件實(shí)現(xiàn)下拉框功能,感興趣的朋友一起看看吧

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

我們先來看一下效果圖

這里寫圖片描述

這里寫圖片描述

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

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

首先,我們先在xml文件中將spinner寫出

<?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來為其提供顯示的數(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("國際米蘭");
  teamList.add("AC米蘭");
 }
}

源碼地址

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

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

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

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

如果開發(fā)者使用Spinner時(shí)已經(jīng)可以確定列表選擇框里的列表項(xiàng),則完全不需要編寫代碼,只要為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);
 }
}


相關(guān)文章

最新評(píng)論