Android進(jìn)階之Spinner下拉框的使用方法
Spinner有倆種實(shí)現(xiàn)方式,除去存儲(chǔ)數(shù)據(jù)與讀取數(shù)據(jù)不同的話(huà),其他并沒(méi)有什么區(qū)別
Effect :
第一種:
在Res目錄下,找到values文件夾下arrays.xml進(jìn)行資源存儲(chǔ)
<string-array name="state"> <item >今日</item> <item >昨日</item> <item >本周</item> <item >上周</item> <item >本月</item> <item >上月</item> </string-array>
在代碼中讀取資源:
String[] mItems = getResources().getStringArray(R.array.state);
第二種:
在代碼中進(jìn)行數(shù)據(jù)源的存儲(chǔ)與讀取,這篇文章中我將使用這種方式進(jìn)行實(shí)現(xiàn)
MainActivity :
package com.example.dow.spinner; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import org.w3c.dom.Text; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private Spinner mSpinner; private TextView mTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSpinner = (Spinner) findViewById(R.id.sp_btn); //只是為了展示我們的實(shí)現(xiàn)效果,故可不要 mTv = (TextView) findViewById(R.id.tv_content); //數(shù)據(jù)源 ArrayList<String> spinners = new ArrayList<>(); spinners.add("今日"); spinners.add("昨日"); spinners.add("本周"); spinners.add("上周"); spinners.add("本月"); spinners.add("上月"); //設(shè)置ArrayAdapter內(nèi)置的item樣式-這里是單行顯示樣式 final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinners); //這里設(shè)置的是Spinner的樣式 , 輸入 simple_之后會(huì)提示有4人,如果專(zhuān)屬spinner的話(huà)應(yīng)該是倆種,在特殊情況可自己定義樣式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_item); //設(shè)置Adapter了 mSpinner.setAdapter(adapter); //監(jiān)聽(tīng)Spinner的操作 mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { //選取時(shí)候的操作 @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mTv.setText(adapter.getItem(position)); } //沒(méi)被選取時(shí)的操作 @Override public void onNothingSelected(AdapterView<?> parent) { mTv.setText("No anything"); } }); } }
MainActivity 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:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dow.spinner.MainActivity"> <Spinner android:padding="5dp" android:layout_gravity="center" android:layout_width="60dp" android:layout_height="wrap_content" android:id="@+id/sp_btn" /> <TextView android:padding="5dp" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_content" /> </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)滑動(dòng)到頂部懸停的效果
這篇文章給大家介紹一種不常見(jiàn)的實(shí)現(xiàn)Android滑動(dòng)到頂部懸停效果的方式,對(duì)大家開(kāi)發(fā)Android具有一定的參考借鑒價(jià)值,有需要的朋友們可以來(lái)一起看看。2016-09-09android dialog邊框去除白色邊框?qū)崿F(xiàn)思路及代碼
android dialog邊框含有白色真是美中不足啊,本文將介紹如何去除白色邊框,有思路及代碼,感興趣的朋友可以了解下2013-01-01Android?NDK開(kāi)發(fā)之FFmpeg視頻添加水印
這篇文章主要介紹了在Android?NDK開(kāi)發(fā)中如何通過(guò)FFmpeg為視頻添加水印,文中的示例代碼講解詳細(xì),對(duì)我們了解Android開(kāi)發(fā)有一定的幫助,感興趣的可以學(xué)習(xí)一下2021-12-12Android手機(jī)開(kāi)發(fā) 控件 TextView文字居中
本文主要介紹Android手機(jī)開(kāi)發(fā)TextView居中的方法,希望能幫到大家。2016-05-05DCloud的native.js調(diào)用系統(tǒng)分享實(shí)例Android版代碼
本文為大家分享了DCloud的native.js如何調(diào)用系統(tǒng)分享功能Android版的實(shí)例代碼,直接拿來(lái)就用2018-09-09Android之Intent附加數(shù)據(jù)的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Android之Intent附加數(shù)據(jù)的兩種實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了添加數(shù)據(jù)到Intent的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求緩存
這篇文章主要介紹了Android 中Volley二次封裝并實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求緩存的相關(guān)資料,希望通過(guò)本文能幫助到大家,徹底會(huì)使用Volley,需要的朋友可以參考下2017-09-09