android combobox控件,Combobox in Android

本文介绍了如何在Android中使用Spinner作为ComboBox替代方案,通过XML设置控件,并利用数组资源展示文本。当需要与Spinner关联更多数据时,可以使用对象填充Spinner,并自定义toString()方法。在ItemSelectedListener中,可以通过位置获取关联数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

An alternate solution to the need to link Customer ID to the selected Item.

To have a simple selector with text you cause make use of the array resources

Setup the Spinner in XML

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:entries="@array/colors"/>

If you need more data linked with the spinner you can use Objects to populate the spinner.

The default functionality of an ArrayAdapter is to call toString() on any object and pass that to the view.

if (item instanceof CharSequence) {

text.setText((CharSequence)item);

} else {

text.setText(item.toString());

}

You can implement toString() in your object and it will display correctly in the spinner. Then to get the data back from the array you can add a handler onto ItemSelected and get the object back from the seed array or the ArrayAdapter.

ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, arrayOfObjects);

spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()

{

@Override

public void onItemSelected(AdapterView> parent, View view, int position, long id)

{

Log.d(arrayOfObjects[position]._id);

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值