这篇文章主要介绍“Android如何实现Spinner下拉列表效果”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android如何实现Spinner下拉列表效果”文章能帮助大家解决问题。
一、以数组作为数据源
这种实现方式比较简单,先上效果图:
xml文件代码;
<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Spinner演示" android:layout_gravity="center_horizontal"/> <TextView android:id="@+id/result" android:text="你的选择:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
MainActivity文件代码:
package com.kiritor.ui_spinner; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { private TextView result = null; private Spinner spinner = null; private ArrayAdapter<String> adapter = null; private static final String [] langurage ={"机器语言","汇编","c语言","c++语言","java语言"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); result = (TextView)findViewById(R.id.result); spinner = (Spinner)findViewById(R.id.spinner); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,langurage); //设置下拉列表风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将适配器添加到spinner中去 spinner.setAdapter(adapter); spinner.setVisibility(View.VISIBLE);//设置默认显示 spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub result.setText("你的选择是:"+((TextView)arg1).getText()); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
二、以xml文件作为数据源
先上效果图吧:
数据源xml文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="langurage"> <item>机器语言</item> <item>汇编语言</item> <item>c语言</item> <item>c++语言</item> <item>java语言</item> <item>Android学习</item> </string-array> </resources>
xml文件:
<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Spinner演示" android:layout_gravity="center_horizontal"/> <TextView android:id="@+id/result" android:text="你的选择:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
MainActivity
package com.kiritor.ui_spinner; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends Activity { private TextView result = null; private Spinner spinner = null; private ArrayAdapter<CharSequence> adapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); result = (TextView)findViewById(R.id.result); spinner = (Spinner)findViewById(R.id.spinner); <span >adapter = ArrayAdapter.createFromResource(this,R.array.langurage,android.R.layout.simple_spinner_item);</span> //设置下拉列表风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将适配器添加到spinner中去 spinner.setAdapter(adapter); spinner.setVisibility(View.VISIBLE); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub result.setText("你的选择是:"+((TextView)arg1).getText()); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
关于“Android如何实现Spinner下拉列表效果”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。