下拉列表框--自定义选项界面样式
l Spinner.getItemAtPosition(Spinner.getSelectedItemPosition());获取下拉列表框的值
l 调用setOnItemSelectedListener()方法,处理下拉列表框被选择事件,把AdapterView.OnItemSelectedListener实例作为参数传入
主界面设计:
<?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="wrap_content">
<Spinner android:id="@+id/spinner"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
下拉列表框每一项的界面样式:stylespinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contentTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#F4FDFF"
/>
代码处理:
public class SpinnerActivity extends Activity {
private static final String TAG = "SpinnerActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
//第二个参数为layout文件在R文件的id,第三个参数为TextView在layout文件的id
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.stylespinner, R.id.contentTextView);
adapter.add("java");
adapter.add("dotNet");
adapter.add("php");
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
Spinner spinner = (Spinner)adapterView;
String itemContent = (String)adapterView.getItemAtPosition(position);
}
@Override
public void onNothingSelected(AdapterView<?> view) {
Log.i(TAG, view.getClass().getName());
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="city_labes">
<item>北京</item>
<item>上海</item>
<item>广州</item>
<item>辽宁</item>
<item>南京</item>
</string-array>
</resources>
在city_data.xml文件中的内容就是以后向Spinner组件之中填充的数据。
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请选择你喜欢的城市:"/>
<Spinner
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/city_labes"/> à 配置要的内容
<TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
此时是通过配置文件完成了所有的内容输入。但是如果要想对下拉列表框进行事件的处理操作,则可以使用选项选中的事件:public void setOnItemSelectedListener (AdapterView.OnItemSelectedListener listener)
package cn.mldn.demo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;
public class MyActivity extends Activity {
public static final String TAG = "MyActivity";
private Spinner city = null;
private TextView msg = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.city = (Spinner) super.findViewById(R.id.city);
this.msg = (TextView) super.findViewById(R.id.msg);
this.city.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
}
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String value = parent.getItemAtPosition(position).toString();
MyActivity.this.msg.setText("选择的内容是:" + value);
}
public void onNothingSelected(AdapterView<?> parent) {
Log.i(TAG, "** 没有选项被选中。");
}
}
}
这个时候的下拉框是通过配置文件完成的,同样下面换另外一种方式实验一下,如果说现在所有的内容都是在程序中固定的好的,那么又如何呢?
<Spinner
android:id="@+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
如果通过程序配置,那么肯定不能在配置文件之中编写entries属性,如果要想利用程序完成,则必须使用如下一个方法,此方法为设置数据封装适配器:
public void setAdapter (SpinnerAdapter adapter)
这个方法是可以将数组(或集合)内容进行转换,这个方法需要一个SpinnerAdapter接口的对象,那么现在既然这个是接口,那么肯定继续找子类,可以使用一个最简单的子类,因为现在所有的内容都是利用了字符串保存的,那么就利用ArrayAdapter<T>子类,这个子类的构造方法:
public ArrayAdapter (Context context, int textViewResourceId, T[] objects)
范例:定义Activity程序,操作内容
package cn.mldn.demo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
public class MyActivity extends Activity {
public static final String TAG = "MyActivity";
private Spinner city = null;
private TextView msg = null;
private String data[] = new String[] { "北京", "上海", "天津", "南京", "南宁" };
private SpinnerAdapter adapter = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.city = (Spinner) super.findViewById(R.id.city);
this.msg = (TextView) super.findViewById(R.id.msg);
this.adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, this.data);
this.city.setAdapter(this.adapter) ;
this.city.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
}
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String value = parent.getItemAtPosition(position).toString();
MyActivity.this.msg.setText("选择的内容是:" + value);
}
public void onNothingSelected(AdapterView<?> parent) {
Log.i(TAG, "** 没有选项被选中。");
}
}
}