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

首頁 > 系統 > Android > 正文

Android編程實現多列顯示的下拉列表框Spinner功能示例

2019-10-22 18:33:38
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現多列顯示的下拉列表框Spinner功能。分享給大家供大家參考,具體如下:

先上張效果圖:

Android,多列顯示,下拉列表框,Spinner

嚴格來說,這并不是一個下拉列表,只不過實現的效果很像是下拉列表

實現原理:

該下拉列表其實是一個AlertDialog.Builder,給它設置了一個以LinearLayout布局的View。該View內使用了一個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. */@Overridepublic 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>

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄄城县| 彰化县| 综艺| 申扎县| 津市市| 沿河| 乌兰县| 安顺市| 安阳县| 惠安县| 图木舒克市| 清流县| 宁乡县| 丹巴县| 马龙县| 石泉县| 雅安市| 额济纳旗| 修水县| 镶黄旗| 伊吾县| 枝江市| 吉林市| 永兴县| 扶余县| 和平区| 八宿县| 石楼县| 仙桃市| 塘沽区| 新源县| 张北县| 田林县| 莱芜市| 嵊州市| 慈溪市| 东乡族自治县| 区。| 广灵县| 西丰县| 石柱|