Android下拉列表框

时间:2023-02-12 08:59:39

        周末翻了翻书,发现才歇了一两个月没看Android,好多看的东西又都还给书了,遂决定将所学的写到博客中,权当笔记(代码为主),巩固所学知识。

        整理的下拉列表框示例代码中,主要分为以下部分:静态绑定下拉框数据、动态绑定下拉框数据、选择时触发的事件以及获得所选的值;

        1、静态绑定下拉框数据

        需要将数据写在xml中,然后设置下拉框的entries属性,则数据自动加载到下拉框中。具体如下:

        在value文件夹中新建cityInfo.xml,xml中写入

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="cityArray">
<item>北京</item>
<item>江苏</item>
<item>浙江</item>
<item>上海</item>
</string-array>
</resources>
        设计页面控件代码:

    <Spinner android:id="@+id/spinnerCityStatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/cityArray"/>

        2、动态绑定下拉框数据

        主要三个步骤,1.获得数据列表;2.填充数据适配器;3.设置下拉框的适配器;

private Spinner spCity = null;
private ArrayAdapter<CharSequence> adapterCity = null;
private static String[] cityInfo={"北京","江苏","浙江","上海"};
//初始化函数中代码如下
this.spCity = (Spinner) super.findViewById(R.id.spinnerCity);
//将数据cityInfo填充到适配器adapterCity中
this.adapterCity = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_dropdown_item, cityInfo);
//设置下拉框的数据适配器adapterCity
this.spCity.setAdapter(adapterCity);

        3、选择时触发的事件

        需要实现OnItemSelectedListener接口的onItemSelected方法,代码如下

//下拉框选择事件
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String city = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "选择的城市是:" + city,
Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}

}

        4、获得下拉框所选的值

String cityStatic = spCityStatic.getSelectedItem().toString();
       项目完整代码:

       前台设计页面activity_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="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/spinnerCityStatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/cityArray"/>
<Spinner
android:id="@+id/spinnerCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnShowCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看选择的结果"/>
</LinearLayout>
        后台页面MainActivity.java代码:

package com.example.spinner0803;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
private Button btnShowCity = null;
private Spinner spCityStatic = null;
private Spinner spCity = null;
private ArrayAdapter<CharSequence> adapterCity = null;
private static String[] cityInfo={"北京","江苏","浙江","上海"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//按钮相关
btnShowCity = (Button) super.findViewById(R.id.btnShowCity);
btnShowCity.setOnClickListener(new OnClickListenerImpl());
//静态实现的下拉框,数据写在cityInfo.xml文件中
this.spCityStatic = (Spinner) super
.findViewById(R.id.spinnerCityStatic);
this.spCityStatic
.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
//动态实现的下拉框,数据在程序中获得,实际项目可能来自数据库等
this.spCity = (Spinner) super.findViewById(R.id.spinnerCity);
this.adapterCity = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_dropdown_item, cityInfo);
this.spCity.setAdapter(adapterCity);
this.spCity.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
}
//按钮点击事件
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
String cityStatic = spCityStatic.getSelectedItem().toString();
String city = spCity.getSelectedItem().toString();
String selectInfo = "第一个选择的城市是:" + city + ",第二个选择的城市是:"
+ cityStatic;
Toast.makeText(MainActivity.this, selectInfo, Toast.LENGTH_LONG)
.show();
}

}
//下拉框选择事件
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String city = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "选择的城市是:" + city,
Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}

}
}
        项目运行截图:

Android下拉列表框

Android下拉列表框

        项目源码下载