1.带一个选择按钮的Spinner(ArrayAdapter)
package com.rex; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; public class SpinnerTest extends Activity{ private Spinner spinner; private static final String[] Numbers = {"1号","2号","3号","4号","5号"}; private ArrayAdapter<String> myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner); spinner = (Spinner)findViewById(R.id.spinner1); myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Numbers); myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(myAdapter); spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(getApplicationContext(), "你选择了:"+Numbers[arg2], 0).show(); arg0.setVisibility(View.VISIBLE); } @Override public void onNothingSelected(AdapterView<?> arg0) { Toast.makeText(getApplicationContext(), "你什么也没选", 0).show(); } }); } }运行结果:
2.带图片的Spinner(BaseAdapter)
package com.rex; import android.app.Activity; import android.os.Bundle; 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.Toast; public class SpinnerContainImage extends Activity{ private Spinner spinner; private static final String[] Numbers = {"1号","2号","3号"}; private MyAdapter myAdapter; int[] imageId={R.drawable.ic_path_node,R.drawable.ic_poi,R.drawable.ic_scene};; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner); spinner = (Spinner)findViewById(R.id.spinner1); myAdapter= new MyAdapter(); spinner.setAdapter(myAdapter); spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(getApplicationContext(), "你选择了:"+Numbers[arg2], 0).show(); arg0.setVisibility(View.VISIBLE); } @Override public void onNothingSelected(AdapterView<?> arg0) { Toast.makeText(getApplicationContext(), "你什么也没选", 0).show(); } }); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return Numbers.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { LinearLayout linearLayout = new LinearLayout(SpinnerContainImage.this); linearLayout.setOrientation(LinearLayout.HORIZONTAL); ImageView imageView = new ImageView(SpinnerContainImage.this); imageView.setImageDrawable(getResources().getDrawable(imageId[position])); linearLayout.addView(imageView); TextView textView = new TextView(SpinnerContainImage.this); textView.setText(""+Numbers[position]); linearLayout.addView(textView); return linearLayout; } } }
运行结果:
3.点击会在选项的左边显示一个图片的Spinner(这个是在这里看到然后实践了一下 http://my.oschina.net/kut/blog/30564)
<?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:padding="10dip"> <ImageView android:id="@+id/icon" android:src="@drawable/ic_path_node" android:layout_marginRight="10dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="invisible"/> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/icon" android:textColor="#000000" android:textSize="22dip"/> </LinearLayout>
Java的代码:
package com.rex; import android.app.Activity; import android.content.res.Resources; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class Spinner2 extends Activity{ public Spinner spinner; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner); Resources res = getResources(); // CharSequence[] platforms = res.getTextArray(R.array.Numbers); String[] items = {"one","two","three","four"}; spinner = (Spinner) findViewById(R.id.spinner1); // ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence> // (this, android.R.layout.simple_spinner_item, platforms){} ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_item, items) { @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { View view = getLayoutInflater().inflate(R.layout.spinner2, parent, false); TextView label = (TextView) view.findViewById(R.id.label); label.setText(getItem(position)); if (spinner.getSelectedItemPosition() == position) { label.setTextColor(Color.YELLOW); view.setBackgroundColor(Color.WHITE); view.findViewById(R.id.icon).setVisibility(View.VISIBLE); } return view; } }; spinner.setAdapter(adapter); } }
运行结果:
4.直接把Spinner中的数据写到String.XMl文件中
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:layout_height="wrap_content" android:id="@+id/spinner1" android:entries="@array/spinner" android:layout_width="match_parent"></Spinner> </LinearLayout>
spinner = (Spinner)findViewById(R.id.spinner1); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, spinner.getSelectedItem().toString(), 0).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { Toast.makeText(MainActivity.this, "你没有选择", 0).show(); } });