android Spinner的使用

來源:互聯網
上載者:User

標籤:

首先是MainActivity

 

package com.example.spinnertest;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.text.TextUtils;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView;import com.example.spinnertest.R;public class MainActivity extends Activity {    private Spinner myS;     private List<String> datas=new ArrayList<String>();    private MySpinnerAdapter sAdapter;    private String value;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        myS = (Spinner)findViewById(R.id.spinnerS);        initData();        sAdapter = new MySpinnerAdapter(this,datas);        myS.setAdapter(sAdapter);        refreshShowView();        myS.setOnItemSelectedListener(new OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<?> arg0, View arg1,                    int arg2, long arg3) {                // TODO Auto-generated method stub                value = sAdapter.getItem(arg2);            }            @Override            public void onNothingSelected(AdapterView<?> arg0) {                // TODO Auto-generated method stub                            }        });    }    private void initData() {        // TODO Auto-generated method stub        datas.add("a");        datas.add("b");        datas.add("c");        datas.add("d");        datas.add("e");        datas.add("f");        value = datas.get(4);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }        class MySpinnerAdapter extends  ArrayAdapter<String> {        private List<String> mList;        private Context mContext;        class ViewHolder {            public TextView spinnerTv;            public ViewHolder(View v){                spinnerTv = (TextView)v.findViewById(R.id.spinner_tv);            }        }        public MySpinnerAdapter(Context context,  List<String> list) {            super(context,R.layout.spinner_item,R.id.spinner_tv, list);            // TODO Auto-generated constructor stub            setData(list);            mContext = context;        }        public void setData(List<String> list){            mList = list;            notifyDataSetChanged();        }        @Override        public int getCount() {            // TODO Auto-generated method stub            if(mList==null){                return 0;            }            return mList.size();        }        @Override        public String getItem(int position) {            // TODO Auto-generated method stub            if(mList==null){                return null;            }            return mList.get(position);        }        @Override        public long getItemId(int position) {            // TODO Auto-generated method stub            return position;        }        @Override        public View getDropDownView(int position, View convertView, ViewGroup parent)        {             ViewHolder holder = null;            if(convertView==null){                convertView = LayoutInflater.from(mContext).inflate(                        R.layout.spinner_item, null);                holder= new ViewHolder(convertView);                convertView.setTag(holder);            }else{                holder = (ViewHolder)convertView.getTag();            }            String myListItem = mList.get(position);             holder.spinnerTv.setText(myListItem);            return convertView;         }            }        private void refreshShowView(){        if(!TextUtils.isEmpty(value)){            int i = 0;            for(String item:datas){                if(value.equals(item)){                    break;                }                i++;            }            if(i==datas.size()){                i = datas.size()-1;            }            myS.setSelection(i);        }    };}

然後是activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="horizontal"    android:gravity="center_vertical"    tools:context=".MainActivity" >    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="標題"/>     <Spinner         android:id="@+id/spinnerS"         android:layout_width="0dp"         android:layout_weight="1"         android:layout_height="wrap_content"/>   </LinearLayout>

然後是spinner_item:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:paddingTop="10dp"        android:paddingBottom="10dp"        android:id="@+id/spinner_tv"/></RelativeLayout>

 

android Spinner的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.