listView页面布局:layout/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:orientation="vertical">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择年份"
android:layout_gravity="center_vertical"/>
<Spinner
android:id="@+id/spYears"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/prompt_year"//1、设置默认的显示,2、prompt [prm(p)t] 【计算机】提示信息的
android:spinnerMode="dialog"//设置下拉的样式
/>
</TableRow>
</TableLayout>
</LinearLayout>
该布局下的java代码:
package com.example.day05_0222;
import android.support.v7.app.ActionBarActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Spinner spYear;//下来选框的对象
private BaseAdapter arrayAdapter;//适配器
private String [] spyearnames;//数据数组
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initData();
setListener();
}
private void initData() {//初始化数据
// TODO Auto-generated method stub
Resources resource = getResources();
spyearnames = resource.getStringArray(R.array.years);
}
private void setListener() {//设置选中后的监听事件
// TODO Auto-generated method stub
spYear.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
switch(parent.getId()){
case R.id.spYears:
String year = spyearnames[position];
Toast.makeText(MainActivity.this, "选中的年份是:"+year, 20000).show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
private void init() {
// TODO Auto-generated method stub
//得到下来选框的对象
spYear = (Spinner) findViewById(R.id.spYears);
//得到适配器对象
//重点解释第一个参数是要显示的布局所在的activity对象,第二个参数是指要显示的集合数据的物理地址,第三个参数是
// 指适配的布局页面,这里直接用了android自带的布局
arrayAdapter = ArrayAdapter.createFromResource(this, R.array.years, android.R.layout.simple_list_item_1);
//设置适配器
spYear.setAdapter(arrayAdapter);
}
}
String字符串资源:values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Day05_0222</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="prompt_year">选择年份</string>
<string-array name="years">
<item>2000</item>
<item>2001</item>
<item>2002</item>
<item>2003</item>
<item>2004</item>
<item>2005</item>
<item>2006</item>
<item>2007</item>
</string-array>
</resources>
效果:
本文出自 “追随心的彼岸” 博客,谢绝转载!
原文地址:http://qa962839575.blog.51cto.com/6819179/1587133