今天就跟大家聊聊有关Android中如何使用Spinner组件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。Android Spinner 组件Spinner: 下拉组件使用事项:布
今天就跟大家聊聊有关Android中如何使用Spinner组件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
Android Spinner 组件
Spinner: 下拉组件
使用事项:布局在XML 中实现,具体的数据在JAVA 代码中实现;
所用知识点:
数组适配器:ArrayAdapter 用于关系M 层和 C 层;
事件:OnItemSelectedListener;
案列:查看十二星座效果图:
xml:代码如下:
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical" > <Spinner android:id="@+id/spinner" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/showInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:gravity="center" android:text=" " /> </LinearLayout> </ScrollView> </LinearLayout>
java代码如下:
package com.example.spinnertest; import java.util.ArrayList; import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { private Spinner spinner; private TextView tx; private ArrayList<String> list = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tx = (TextView) findViewById(R.id.showInfo); spinner = (Spinner) findViewById(R.id.spinner); list = new ArrayList<String>(); list.add("Aries"); list.add("Taurus"); list.add("Gemini"); list.add("Cancer"); list.add("Leo"); list.add("VirGo"); list.add("Libra"); list.add("Scorpio"); list.add("Sagittarius"); list.add("Capricorn"); list.add("Aquarius"); list.add("Pisces"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int id, long position) { // Toast.makeText(MainActivity.this, "你选择的是第:"+id+"值为:"+list.get(id), 1000).show(); String temp = getConstellation(id); tx.setText("\t"+temp); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } protected String getConstellation(int id){ Resources rs = getResources(); String temp = ""; switch(id){ case 0: temp = rs.getString(R.string.Aries); break; case 1: temp = rs.getString(R.string.Taurus); break; case 2: temp = rs.getString(R.string.Gemini); break; case 3: temp = rs.getString(R.string.Cancer); break; case 4: temp = rs.getString(R.string.Leo); break; case 5: temp = rs.getString(R.string.Virgo); break; case 6: temp = rs.getString(R.string.Libra); break; case 7: temp = rs.getString(R.string.Scorpio); break; case 8: temp = rs.getString(R.string.Sagittarius); break; case 9: temp = rs.getString(R.string.Capricorn); break; case 10: temp = rs.getString(R.string.Aquarius); break; case 11: temp = rs.getString(R.string.Pisces); break; case 12: temp = rs.getString(R.string.Aries); break; } return temp; } }
看完上述内容,你们对Android中如何使用Spinner组件有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网精选频道,感谢大家的支持。
--结束END--
本文标题: Android中如何使用Spinner组件
本文链接: https://www.lsjlt.com/news/221274.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0