Android編程實(shí)現(xiàn)多列顯示的下拉列表框Spinner功能示例
本文實(shí)例講述了Android編程實(shí)現(xiàn)多列顯示的下拉列表框Spinner功能。分享給大家供大家參考,具體如下:
先上張效果圖:

嚴(yán)格來(lái)說(shuō),這并不是一個(gè)下拉列表,只不過(guò)實(shí)現(xiàn)的效果很像是下拉列表
實(shí)現(xiàn)原理:
該下拉列表其實(shí)是一個(gè)AlertDialog.Builder,給它設(shè)置了一個(gè)以LinearLayout布局的View。該View內(nèi)使用了一個(gè)GridView組件,
代碼:
package yyy.testandroid9;
private Intent intent;
private int index = 0;
private SharedPreferences preferences;
private File file;
private Spinner spinner;
private GridView gridView;
private LinearLayout layout;
private Builder builder;
private AlertDialog dialog;
private MyAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = LayoutInflater.from(this);
layout = (LinearLayout) inflater.inflate(R.layout.bank_grid, null);
gridView = (GridView) layout.findViewById(R.id.grid);
adapter = new MyAdapter(this);
gridView.setAdapter(adapter);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
textView = (TextView) findViewById(R.id.textview);
builder = new Builder(TestAndroid9Activity.this);
builder.setView(layout);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(dialog == null){
dialog = builder.show();
}
dialog.show();
}
});
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
button1.setText(getResources().getStringArray(R.array.city)[position]);
dialog.dismiss();
adapter.map.put(index, false);
adapter.map.put(position, true);
index = position;
adapter.notifyDataSetChanged();
}
});
}
private class MyAdapter extends BaseAdapter{
private Context context;
private String[] citys;
private LayoutInflater inflater;
public HashMap<Integer, Boolean> map;
public MyAdapter(Context context) {
super();
this.context = context;
citys = context.getResources().getStringArray(R.array.city);
inflater = LayoutInflater.from(context);
map = new HashMap<Integer, Boolean>();
for(int i=0;i<citys.length;i++){
map.put(i, false);
}
}
public int getCount() {
// TODO Auto-generated method stub
return citys.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
if(view == null){
view = inflater.inflate(R.layout.bank_item, null);
}
TextView textView = (TextView) view.findViewById(R.id.list_text);
RadioButton radioButton = (RadioButton) view.findViewById(R.id.radiobutton);
radioButton.setChecked(map.get(position));
textView.setText(citys[position]);
return view;
}
}
bank_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp"
android:background="#ffffff"
android:id="@+id/grid_layout">
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="20px"
android:horizontalSpacing="10px"
android:numColumns="4"
android:scrollbars="vertical"
android:layout_margin="10dp"/>
</LinearLayout>
bank_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<RadioButton
android:id="@+id/radiobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
/>
<TextView
android:id="@+id/list_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_gravity="center_vertical"/>
</LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android使用Spinner控件實(shí)現(xiàn)下拉列表的案例
- Android控件Spinner實(shí)現(xiàn)下拉列表及監(jiān)聽(tīng)功能
- Android UI控件之Spinner下拉列表效果
- Android自定義單選多選下拉列表的實(shí)例代碼
- Android下拉列表選項(xiàng)框及指示箭頭動(dòng)畫(huà)
- Android中PopuWindow實(shí)現(xiàn)下拉列表實(shí)例
- Android中使用Spinner實(shí)現(xiàn)下拉列表功能
- Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能
- 詳解Android的Socket通信、List加載更多、Spinner下拉列表
- Android下拉列表框Spinner使用方法詳解
相關(guān)文章
Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用
這篇文章主要介紹了Android DrawerLayout抽屜布局與NavigationView導(dǎo)航菜單應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
android開(kāi)發(fā)教程之清除android數(shù)據(jù)緩存示例(清除本地?cái)?shù)據(jù)緩存)
android清除本地?cái)?shù)據(jù)緩存示例,主要功能清除內(nèi)/外緩存,清除數(shù)據(jù)庫(kù),清除sharedPreference,清除files和清除自定義目錄,需要的朋友可以參考下2014-02-02
Android開(kāi)發(fā)利器之pidcat安裝方式
pidcat 是Android屆JakeWharton大神開(kāi)發(fā)的一款命令行工具,堪稱Android開(kāi)發(fā)利器,它能方便Android程序猿捕獲日志,過(guò)濾日志,定位程序問(wèn)題,超級(jí)好用。這篇文章給大家介紹了Android開(kāi)發(fā)利器之pidcat,需要的朋友可以參考下2019-05-05
Android PC投屏功能實(shí)現(xiàn)的示例代碼
本篇文章主要介紹了Android PC投屏功能實(shí)現(xiàn)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
android studio生成aar包并在其他工程引用aar包的方法
本篇文章主要介紹了android studio生成aar包并在其他工程引用aar包的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別
這篇文章主要介紹了淺析Android中g(shù)etWidth()和getMeasuredWidth()的區(qū)別 ,getMeasuredWidth()獲取的是view原始的大小,getWidth()獲取的是這個(gè)view最終顯示的大小,具體區(qū)別介紹大家參考下本文2018-04-04
Android TextView Marquee的應(yīng)用實(shí)例詳解
這篇文章主要介紹了Android TextView Marquee的應(yīng)用實(shí)例詳解的相關(guān)資料,這里說(shuō)明使用方法及簡(jiǎn)單實(shí)例和注意實(shí)現(xiàn),需要的朋友可以參考下2017-08-08
Android SwipeRefreshLayout下拉刷新源碼解析
這篇文章主要為大家詳細(xì)解析了Android SwipeRefreshLayout下拉刷新源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Flutter數(shù)字切換動(dòng)畫(huà)實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Flutter數(shù)字切換動(dòng)畫(huà)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

