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>

效果:


本文出自 “追随心的彼岸” 博客,谢绝转载!

更多相关文章

  1. Android-线性布局的经典案例1-计算器
  2. Android简易计算器——LinearLayout布局
  3. android 使用代码实现 RelativeLayout布局
  4. Android从零撸美团(四) - 美团首页布局解析及实现 - Banner+自定
  5. 源码解析Android中View的layout布局过程
  6. 用android LinearLayout和RelativeLayout实现精确布局
  7. Android中微信主界面菜单栏的布局实现代码

随机推荐

  1. Android 如何在IDEA Eclipse 的UI Editor
  2. android 重力感应小球 Demo
  3. Android(安卓)文件在SDCard上读写操作
  4. ch01Android架构体系
  5. android面试(2)----组件
  6. Android屏幕尺寸
  7. Android布局详解之二:LinearLayout
  8. [Android] Failed to allocate memory: 8
  9. android IPC学习
  10. android top弹出框