spinner的用法

spinner是一个下拉菜单,这里我就用一个例子来解释。

首先我们先把数据添加到Spinner

spinner = (Spinner) findViewById(R.id.spinner);
		List<String> list = new ArrayList<String>();
		list.add("中文");
		list.add("英文");
		// 第一个参数是上下文
		// 第二个参数spinner点击出现一列的布局
		// 第三个参数就是那列text的id
		// 第四个就是要添加的数据
		ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.line,
				R.id.line_view, list);
		spinner.setAdapter(adapter);

		spinner.setOnItemSelectedListener(this);
当把数据添加到spinner之后我们就设置点击事件,通过点击事件让我们在spinner的界面上显示你选定的内容

// 第一个参数adapterview就是整个列表的一个对象
	// 第二个参数view就是被选中的条目的view
	// 第四个就是第几个被选中的条目的数字
	// 第五个就是选中控件的id
	@Override
	public void onItemSelected(AdapterView<?> adapterview, View view,
			int position, long id) {
		// mm这里就是spinner界面上显示的字体
		String mm = adapterview.getItemAtPosition(position).toString();

		

	}

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

	}
下面介绍一下用到的布局,主界面也就一个spinner

<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"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="40dp" />

</LinearLayout>

最后说下line布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/line_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值