Android控件之Spinner用法實(shí)例分析
本文實(shí)例講述了Android控件之Spinner用法。分享給大家供大家參考。具體如下:
以下模擬下拉列表的用法
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:text="@string/ys" android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="28dip" /> <Spinner android:id="@+id/Spinner01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
SpinnerActivity類:
package com.ljq.sp; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.AdapterView.OnItemSelectedListener; public class SpinnerActivity extends Activity { private Spinner sp = null;//下拉列表 private TextView tv = null; // 所有資源圖片的數(shù)組 private int[] drawableIds={R.drawable.football,R.drawable.basketball,R.drawable.volleyball}; // 所有字符串的數(shù)組 private int[] msgIds={R.string.zq,R.string.lq,R.string.pq}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.TextView01); sp=(Spinner)this.findViewById(R.id.Spinner01);//初始化Spinner sp.setAdapter(adapter); sp.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int positon, long id) { LinearLayout ll = (LinearLayout) view; View v=ll.getChildAt(0);//獲取第一個(gè)控件ImageView Log.i("ljq", v.getClass().getName()); TextView tvn = (TextView) ll.getChildAt(1);//獲取第二個(gè)控件TextView StringBuilder sb = new StringBuilder(); sb.append(getResources().getText(R.string.ys)).append(":").append(tvn.getText()); tv.setText(sb.toString()); } public void onNothingSelected(AdapterView<?> parent) { } }); } private BaseAdapter adapter = new BaseAdapter(){ public int getCount() { return drawableIds.length; } public Object getItem(int position) { return drawableIds[position]; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(SpinnerActivity.this); ll.setOrientation(LinearLayout.HORIZONTAL); ImageView iv = new ImageView(SpinnerActivity.this); iv.setImageResource(drawableIds[position]); ll.addView(iv); TextView tv=new TextView(SpinnerActivity.this); tv.setText(msgIds[position]);//設(shè)置內(nèi)容 tv.setTextSize(24); tv.setTextColor(R.color.black); ll.addView(tv); return ll; } }; }
運(yùn)行結(jié)果
希望本文所述對(duì)大家的Android序設(shè)計(jì)有所幫助。
相關(guān)文章
Android+Flutter實(shí)現(xiàn)彩虹圖案的繪制
彩虹,是氣象中的一種光學(xué)現(xiàn)象,當(dāng)太陽光照射到半空中的水滴,光線被折射及反射,在天空上形成拱形的七彩光譜。接下來,我們就自己手動(dòng)繪制一下彩虹圖案吧2022-11-11Android封裝實(shí)現(xiàn)短信驗(yàn)證碼的獲取倒計(jì)時(shí)
這篇文章主要介紹了Android封裝實(shí)現(xiàn)短信驗(yàn)證碼的獲取倒計(jì)時(shí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09安卓GET與POST網(wǎng)絡(luò)請(qǐng)求的三種方式
今天小編就為大家分享一篇關(guān)于安卓GET與POST網(wǎng)絡(luò)請(qǐng)求的三種方式,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Android自動(dòng)提示控件AutoCompleteTextView
這篇文章主要介紹了Android自動(dòng)提示控件AutoCompleteTextView的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致的實(shí)例代碼
下面小編就為大家分享一篇仿ios狀態(tài)欄顏色和標(biāo)題欄顏色一致的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android開發(fā)之使用150行代碼實(shí)現(xiàn)滑動(dòng)返回效果
本文給大家分享Android開發(fā)之使用150行代碼實(shí)現(xiàn)滑動(dòng)返回效果的代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-05-05