欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 控件之下拉框—Spinner控件

程序员文章站 2023-12-28 12:00:04
...
一.Spinner概述 
Spinner是一个下拉框的控件,当用户选择的时候,可以提供一个下拉列表将所有可选的项列出来,每次只能选择一项。它的项来自于与之相关联的适配器中。Spinner的使用,可以极大提高用户的体验性。
二.Spinner使用(有两种提供数据源的方式:数组和xml)
1.新建一个android的工程
2.在layout文件夹下新建一个spinnerlayout.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/strColor"/>
<Spinner android:id="@+id/spinnerColor"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Spinner>
<TextView android:id="@+id/tv"
android:layout_width="200dip"
android:layout_height="20dip"></TextView>

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/strSeason"></TextView>
<Spinner android:id="@+id/spinnerSeason"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></Spinner>
</LinearLayout>

java代码:

package yang.SpinnerDemo;

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

public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Spinner spinnerColor;
private Spinner spinnerSeason;
private TextView tv;

private static final String[] colors={"红色","黄色","蓝色","绿色"};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerlayout);

tv=(TextView)findViewById(R.id.tv);
//获取spinnerColor对象
spinnerColor=(Spinner)findViewById(R.id.spinnerColor);
//将可选内容与ArrayAdapter连接起来
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.main, colors);
//将adapter 添加到spinner中
spinnerColor.setAdapter(adapter);
//添加Spinner事件监听器
spinnerColor.setOnItemSelectedListener(new ColorListener());


//获取spinnerSeason对象
spinnerSeason=(Spinner)findViewById(R.id.spinnerSeason);
ArrayAdapter<CharSequence> adapter2=ArrayAdapter.createFromResource(this,R.array.season, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//设置下拉列表的风格
spinnerSeason.setAdapter(adapter2);
spinnerSeason.setOnItemSelectedListener(new SeasonListener());
}

private class ColorListener implements OnItemSelectedListener{

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
tv.setText("您选择的颜色是:"+arg2);
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}
private class SeasonListener implements OnItemSelectedListener{

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//Toast用于向用户显示一些帮助/提示。
Toast.makeText(MainActivity.this, "Spinner2的位置:"+arg2,Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}
}

xml源文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="season">
<item>春天</item>
<item>夏天</item>
<item>秋天</item>
<item>冬天</item>
</string-array>
</resources>

源文件:
相关标签: Android XML OS

上一篇:

下一篇: