在Android平台上使用下拉列表用到了Spinner组件,效果图如下:
?
代码:
1,res/values/string.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="title_tx">团购人群调查:你所处在年龄段:</string> <string name="app_name">下拉列表(Spinner)</string> <string name="about_80080088">更多详情,可登录http://www.80080088.com了解。</string></resources>
?
2,res/layout/main.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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title_tx" android:id="@+id/title_tx"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/about_80080088" android:id="@+id/about_80080088"> </TextView> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinnerAge"> </Spinner></LinearLayout>
?
3.Activity类
package com.cottsoft.android;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView;public class A008Activity extends Activity { private static final String[] m_Ages = {"0到18岁","19至30岁","31到60岁","61岁到80岁","81岁以上"}; private TextView m_title_tx,m_about_80080088; private Spinner m_Spinner; private ArrayAdapter<String> adapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); m_title_tx = (TextView) findViewById(R.id.title_tx); m_about_80080088=(TextView) findViewById(R.id.about_80080088); m_Spinner = (Spinner) findViewById(R.id.spinnerAge); m_title_tx.setTextSize(22); m_about_80080088.setTextSize(15); m_about_80080088.setTextColor(Color.GRAY); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,m_Ages); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); m_Spinner.setAdapter(adapter); m_Spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { m_title_tx.setText(getResources().getString(R.string.title_tx)+m_Ages[arg2]); arg0.setVisibility(View.VISIBLE); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); }}
?