Android編程開發(fā)之Spinner組件用法
本文實例講述了Android編程開發(fā)之Spinner組件用法。分享給大家供大家參考,具體如下:
Spinner組件組要用顯示一個下拉列表,在使用中需要用到適配器Adapter,下面是一個該組件的使用示例
首先是布局文件main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Spinner android:id="@+id/spinner1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/spinner2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp"/> </LinearLayout>
由于用到simpAdapter所以要寫子項Item的布局如下 item.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/ivLogo" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/icon" android:paddingLeft="10dp" /> <TextView android:id="@+id/tvApplicationName" android:textColor="#000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textSize="16dp" android:gravity="center_vertical" android:paddingLeft="10dp" /> </LinearLayout>
下面是代碼:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class Main extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲取對象
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
String[] applicationNames = new String[]
{ "多功能日歷", "eoeMarket客戶端", "耐玩的重力消磚塊", "白社會", "程序終結(jié)者" };
ArrayAdapter<String> aaAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, applicationNames);
// 將如下代碼可以使列表項帶RadioButton組件
// aaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(aaAdapter);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
final List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
Map<String, Object> item1 = new HashMap<String, Object>();
item1.put("ivLogo", R.drawable.calendar);
item1.put("tvApplicationName", "多功能日歷");
Map<String, Object> item2 = new HashMap<String, Object>();
item2.put("ivLogo", R.drawable.eoemarket);
item2.put("tvApplicationName", "eoeMarket客戶端");
items.add(item1);
items.add(item2);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, items,
R.layout.item, new String[]
{ "ivLogo", "tvApplicationName" }, new int[]
{ R.id.ivLogo, R.id.tvApplicationName });
spinner2.setAdapter(simpleAdapter);
//為Spinner2加上監(jiān)聽事件
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
new AlertDialog.Builder(view.getContext()).setTitle(
items.get(position).get("tvApplicationName")
.toString()).setIcon(
Integer.parseInt(items.get(position).get("ivLogo")
.toString())).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
}
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android下拉列表spinner的實例代碼
- 學(xué)習(xí)Android自定義Spinner適配器
- Android實現(xiàn)下拉菜單Spinner效果
- Android編程下拉菜單spinner用法小結(jié)(附2則示例)
- Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實現(xiàn))
- Android中Spinner控件之鍵值對用法實例分析
- Android實現(xiàn)聯(lián)動下拉框 下拉列表spinner的實例代碼
- Android Spinner 下拉菜單的使用
- android 之Spinner下拉菜單實現(xiàn)級聯(lián)
- Android編程開發(fā)之Spinner控件用法實例分析
- Android spinner下垃菜單用法實例詳解
相關(guān)文章
Flutter 中的PageStorage小部件使用及最佳實踐
在Flutter中,PageStorage小部件提供了一種方法來保存和恢復(fù)頁面間的信息,這對于具有多個頁面且需要在這些頁面之間共享狀態(tài)的應(yīng)用程序非常有用,本文將詳細(xì)介紹PageStorage的用途、如何使用它以及一些最佳實踐,感興趣的朋友跟隨小編一起看看吧2024-05-05
Android使用viewpager實現(xiàn)自動無限輪播圖
這篇文章主要介紹了Android使用viewpager實現(xiàn)自動無限輪播圖效果,實現(xiàn)方法大概有兩種,一種是viewpager+作為游標(biāo)的點 。另外一種是重寫viewpager,具體實現(xiàn)過程大家參考下本文2018-06-06
Android之日期時間選擇控件DatePicker和TimePicker實例
本篇文章主要介紹了Android之日期時間選擇控件DatePicker和TimePicker實例,具有一定的參考價值,有興趣的可以了解一下2017-05-05
詳解 android 光線傳感器 light sensor的使用
這篇文章主要介紹了詳解 android 光線傳感器 light sensor的使用的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android中通過訪問本地相冊或者相機設(shè)置用戶頭像實例
本篇文章主要介紹了Android中通過訪問本地相冊或者相機設(shè)置用戶頭像,具有一定的參考價值,有興趣的可以了解一下。2017-01-01
Android開發(fā)中Intent.Action各種常見的作用匯總
今天小編就為大家分享一篇關(guān)于Android開發(fā)中Intent.Action各種常見的作用匯總,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

