国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android編程開(kāi)發(fā)之Spinner組件用法

2020-04-11 11:08:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Android編程開(kāi)發(fā)之Spinner組件用法。分享給大家供大家參考,具體如下:

Spinner組件組要用顯示一個(gè)下拉列表,在使用中需要用到適配器Adapter,下面是一個(gè)該組件的使用示例

首先是布局文件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所以要寫(xiě)子項(xiàng)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);   //獲取對(duì)象   Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);   String[] applicationNames = new String[]   { "多功能日歷", "eoeMarket客戶端", "耐玩的重力消磚塊", "白社會(huì)", "程序終結(jié)者" };   ArrayAdapter<String> aaAdapter = new ArrayAdapter<String>(this,     android.R.layout.simple_spinner_item, applicationNames);   // 將如下代碼可以使列表項(xiàng)帶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)聽(tīng)事件   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)    {   }   });  } }

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 六枝特区| 合肥市| 东莞市| 香港 | 监利县| 望城县| 平山县| 如皋市| 霍邱县| 刚察县| 青龙| 横峰县| 南开区| 通城县| 翁源县| 慈利县| 广平县| 东兴市| 南召县| 泽州县| 永寿县| 双峰县| 鸡东县| 秀山| 武宣县| 河池市| 赤城县| 额尔古纳市| 南昌县| 郁南县| 吉安县| 洛南县| 闵行区| 射阳县| 调兵山市| 霍林郭勒市| 桐梓县| 宣化县| 新河县| 泽州县| 嘉义县|