接下来我将以以下顺序来讲解spinner是如何使用的
一、xml界面代码解释
1.一些属性讲解
二、java代码解释
1.没有绑定adapter
2.有绑定adapter
3.ArrayList讲解
4.利用ArrayList来创建下拉菜单
一、
在 后缀为 .xml 文件里
首先我先这个就是sprinner控件
接下来我先讲解属性
1.
android
:entries=
"@array/languages" 这里是引用在values/array里的数组 它的代码如下:
xml version=
"1.0"
encoding=
"utf-8"
?>
<
resources
>
<
string-array
name=
"languages"
>
<
item
>c语言
item
>
<
item
>java
item
>
<
item
>php
item
>
<
item
>xml
item
>
<
item
>html
item
>
string-array
>
resources>
2.
android
:spinnerMode=
"dropdown" dropdown 是出来的是下拉框 dialog 是出来一个覆盖全屏的对话框
如下图:
对话框
下拉框
接下来给出所有 xml代码 (重点代码会标记为红色)
MainActivity.xml代码
xml version=
"1.0"
encoding=
"utf-8"
?>
<
LinearLayout
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
android
:orientation=
"vertical"
xmlns:
tools
=
"http://schemas.android.com/tools"
android
:id=
"@+id/activity_main"
android
:layout_width=
"match_parent"
android
:layout_height=
"match_parent"
tools
:context=
"com.example.asus.spinner.MainActivity"
>
<
Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/languages"
android:spinnerMode="dropdown"
/>
<
Spinner
android
:layout_width=
"wrap_content"
android
:layout_height=
"wrap_content"
android
:id=
"@+id/spinner2"
>
Spinner
>
<
Spinner
android
:layout_width=
"wrap_content"
android
:layout_height=
"wrap_content"
android
:id=
"@+id/spinner3"
android
:spinnerMode=
"dropdown"
android
:dropDownWidth=
"fill_parent"
>
Spinner
>
LinearLayout
>
arryy.xml
xml version=
"1.0"
encoding=
"utf-8"
?>
<
resources
>
<
string-array
name=
"languages"
>
<
item
>c语言
item
>
<
item
>java
item
>
<
item
>php
item
>
<
item
>xml
item
>
<
item
>html
item
>
string-array
>
resources>
二、(这里用的xml代码可以用上面的来)
1.没有绑定adapter
Spinner spinner=(Spinner)findViewById(R.id.
spinner1
);
spinner.setOnItemSelectedListener(
new
AdapterView.OnItemSelectedListener() {
@Override
public void
onItemSelected(AdapterView> parent, View view,
int
position,
long
id) {
String[] languages1=getResources().getStringArray(
languages
);
Toast.
makeText
(MainActivity.
this
,
"你点击的是"
+languages1[position],Toast.
LENGTH_LONG
).show();
}
@Override
public void
onNothingSelected(AdapterView> parent) {
}
});
2.有绑定adapter
Spinner spinner2=(Spinner)findViewById(R.id.
spinner2
);
String [] mItems=getResources().getStringArray(
languages
);
ArrayAdapter adapter1=
new
ArrayAdapter(
this
,R.layout.
support_simple_spinner_dropdown_item
,mItems);
adapter1.setDropDownViewResource(R.layout.
support_simple_spinner_dropdown_item
);
//有这句与没有这句显示出来的菜单样式不一样,没有就会和原本的按钮一个样式
spinner2.setAdapter(adapter1);
spinner2.setOnItemSelectedListener(
new
AdapterView.OnItemSelectedListener() {
@Override
public void
onItemSelected(AdapterView> parent, View view,
int
position,
long
id) {
String[] languages2=getResources().getStringArray(
languages
);
Toast.
makeText
(MainActivity.
this
,
"你点击的是"
+languages2[position],Toast.
LENGTH_LONG
).show();
}
@Override
public void
onNothingSelected(AdapterView> parent) {
}
});
3.ArrayList 浅谈
ArrayList 其实就是一个类,你可以把它想象成一个线性表,只不过是中间不能有NULL。
ArrayList<类> 这里面是自定义的类,表示这个线性表里要添加的类 (其实ArrayList 就是一张线性表而已,它提供了函数来操作)
下面的例子因为String类他有自定义的类来接受,所以有add(int,string),remove(int);
4.利用arraylist来创建下拉菜单
Spinner spinner3 =(Spinner)findViewById(R.id.
spinner3
);
final
ArrayListperson=
new
ArrayList();
person.add(
0
,
"王泽"
);
person.add(
1
,
"神人"
);
person.add(
2
,
"俞少"
);
person.remove(
1
);
ArrayAdapter adapter=
new
ArrayAdapter(
this
,R.layout.
support_simple_spinner_dropdown_item
,person);
spinner3.setAdapter(adapter);
spinner3.setOnItemSelectedListener(
new
AdapterView.OnItemSelectedListener() {
@Override
public void
onItemSelected(AdapterView> parent, View view,
int
position,
long
id) {
Toast.
makeText
(MainActivity.
this
,
"你选择了"
+
person
.get(position),Toast.
LENGTH_LONG
).show();
}
@Override
public void
onNothingSelected(AdapterView> parent) {
}
});
下面是全部.java代码
MainActivity.java
package
com.example.asus.spinner;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.AdapterView;
import
android.widget.ArrayAdapter;
import
android.widget.Spinner;
import
android.widget.Toast;
import
java.util.ArrayList;
import static
com.example.asus.spinner.R.array.
languages
;
public class
MainActivity
extends
Activity {
@Override
protected void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
//不用adapter 绑定 spinner
Spinner spinner=(Spinner)findViewById(R.id.
spinner1
);
spinner.setOnItemSelectedListener(
new
AdapterView.OnItemSelectedListener() {
@Override
public void
onItemSelected(AdapterView> parent, View view,
int
position,
long
id) {
String[] languages1=getResources().getStringArray(
languages
);
Toast.
makeText
(MainActivity.
this
,
"你点击的是"
+languages1[position],Toast.
LENGTH_LONG
).show();
}
@Override
public void
onNothingSelected(AdapterView> parent) {
}
});
//利用adapter 绑定 spinner
Spinner spinner2=(Spinner)findViewById(R.id.
spinner2
);
String [] mItems=getResources().getStringArray(
languages
);
ArrayAdapter adapter1=
new
ArrayAdapter(
this
,R.layout.
support_simple_spinner_dropdown_item
,mItems);
adapter1.setDropDownViewResource(R.layout.
support_simple_spinner_dropdown_item
);
//有这句与没有这句显示出来的菜单样式不一样,没有就会和原本的按钮一个样式
spinner2.setAdapter(adapter1);
spinner2.setOnItemSelectedListener(
new
AdapterView.OnItemSelectedListener() {
@Override
public void
onItemSelected(AdapterView> parent, View view,
int
position,
long
id) {
String[] languages2=getResources().getStringArray(
languages
);
Toast.
makeText
(MainActivity.
this
,
"你点击的是"
+languages2[position],Toast.
LENGTH_LONG
).show();
}
@Override
public void
onNothingSelected(AdapterView> parent) {
}
});
Spinner spinner3 =(Spinner)findViewById(R.id.
spinner3
);
final
ArrayListperson=
new
ArrayList();
person.add(
0
,
"王泽"
);
person.add(
1
,
"神人"
);
person.add(
2
,
"俞少"
);
person.remove(
1
);
ArrayAdapter adapter=
new
ArrayAdapter(
this
,R.layout.
support_simple_spinner_dropdown_item
,person);
spinner3.setAdapter(adapter);
spinner3.setOnItemSelectedListener(
new
AdapterView.OnItemSelectedListener() {
@Override
public void
onItemSelected(AdapterView> parent, View view,
int
position,
long
id) {
Toast.
makeText
(MainActivity.
this
,
"你选择了"
+
person
.get(position),Toast.
LENGTH_LONG
).show();
}
@Override
public void
onNothingSelected(AdapterView> parent) {
}
});
}
}