Spinner初步了解

下拉列表选择框

常用属性:
    android:spinnerMode:列表显示的模式,有两个选择弹出(dialog)或者下拉(dropdown)
    android:entries:使用资源配置数据源
    android:promot:对当前下拉列表设置标题,仅在dialog模式下有效。值必须在@string中设置
    android:dropDownHorizontalOffset:两个方向各种偏移,你懂的,仅在dropdown模式下有用,注意此时包裹内容时的高和宽,例如不能在水平向下偏移的时候设置高是"match_parent"
    android:popupBackGrund:在spinner="dropDown"时,使用这个属性来设置下拉列表的背景。
    

常用方法:
    spinner.setOnItemSelectedListener(new OnItemSelectedListener(){})

1、使用entries资源给Spinner配置数据源的时候需要在string 中添加以下属性string-array
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
<string name="app_name">day033Spinner</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="girls">
<item >德玛西亚</item>
<item >寒冰猪手</item>
<item >无极剑神</item>
<item >剑姬</item>
<item >剑魔</item>
<item >剑豪</item>
</string-array>
 
</resources>
2、在java文件中添加资源给spinner设置一个适配器spinner.setAdapter()
    |--ArrayAdapter第一个方式,只能放单一的文本 构造方法见代码
    |--SimpleAdapter可以添加文本图片混合的 很吊的  构造方法见代码,有点难啊

代码示意:
    activity_main代码示意:
<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"
tools:context="com.mixm.activity.MainActivity" >
 
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="从资源文件中拿到的" />
<Spinner
android:spinnerMode="dialog"
android:id="@+id/sp_1"
android:prompt="@string/choose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/girls"
/>
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
/>
<View
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="来自ArrayAdapter" />
<Spinner
android:spinnerMode="dropdown"
android:id="@+id/sp_2"
android:dropDownVerticalOffset="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
/>
<View
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="来自Adapter" />
<Spinner
android:id="@+id/sp_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
/>
<View
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="玩玩属性" />
<Spinner
android:id="@+id/sp_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/girls"
android:spinnerMode="dropdown"
android:dropDownWidth="60dp"
android:popupBackground="#00ff00"
/>
 
<View
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置第一个" />
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置"
/>
</LinearLayout>
activity_main2代码示意:
<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="horizontal"
tools:context="com.mixm.activity.MainActivity" >
 
<ImageView
android:id="@+id/iv_pic_"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher"
android:scaleType="fitXY"
/>
<TextView
android:id="@+id/tv_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:text="笑话"
/>
 
</LinearLayout>
MainActivity代码示意:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
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.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
 
public class MainActivity extends Activity {
private Spinner sp_1;
private TextView tv_1;
private Spinner sp_2;
private TextView tv_2;
private Spinner sp_3;
private TextView tv_3;
private List<String> girls = new ArrayList<String>();
private Button btn_1;
private SimpleAdapter infos;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
addListener();
}
 
//添加事件监听
private void addListener() {
// TODO Auto-generated method stub
sp_1.setOnItemSelectedListener(new OnItemSelectedListener() {
 
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tv_1.setText(parent.getClass().toString()+view.getClass().toString()+position+"id:"+id);
}
 
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
sp_2.setOnItemSelectedListener(new OnItemSelectedListener() {
 
//下拉框有数据的时候调用
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tv_2.setText(parent.getClass().toString()+view.getClass().toString()+position+"id:"+id);
}
 
//当下拉框中没有数据的时候调用
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//给按钮添加监听事件
btn_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//设置哪个选项被选中
sp_1.setSelection(2);
}
});
}
 
//初始化
private void initView() {
sp_1 = (Spinner) findViewById(R.id.sp_1);
tv_1 = (TextView) findViewById(R.id.tv_1);
sp_2 = (Spinner) findViewById(R.id.sp_2);
tv_2 = (TextView) findViewById(R.id.tv_2);
sp_3 = (Spinner) findViewById(R.id.sp_3);
tv_3 = (TextView) findViewById(R.id.tv_3);
btn_1 = (Button) findViewById(R.id.btn_1);
//给spinner2设置那个啥
girls.add("小红");
girls.add("小花");
girls.add("小丽");
girls.add("嫂子");
girls.add("小黑");
/**
* 给第二个添加适配器
*/
//声明一个适配器 这儿的构造方法很多 不用担心
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,girls);
//设置适配器
sp_2.setAdapter(adapter);
/**
* 给第三个添加适配
*/
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
String[] names = {"小红","小黑","小丽","小白","小花"};
int[] pics = {R.drawable.png1,R.drawable.png2,R.drawable.png3,R.drawable.png4,R.drawable.png5};
for (int i = 0; i < names.length; i++) {
HashMap<String, Object> temp = new HashMap<String, Object>();
temp.put("name", names[i]);
temp.put("pic", pics[i]);
data.add(temp);
}
//这儿的位置 一定要一对一
String[] from = {"pic", "name"};
int[] to = {R.id.iv_pic, R.id.tv_name};
infos = new SimpleAdapter(this, data, R.layout.spinner3_item, from, to);
sp_3.setAdapter(infos);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值