这篇文章主要介绍“Android如何实现Spinner下拉列表效果”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android如何实现Spinner下拉列表效果”文章能帮助大家解决问题。
创新互联专注于白河企业网站建设,响应式网站,商城开发。白河网站建设公司,为白河等地区提供建站服务。全流程定制网站设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
一、以数组作为数据源
这种实现方式比较简单,先上效果图:
xml文件代码;
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 ArrayAdapteradapter = 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 (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文件:
- 机器语言
- 汇编语言
- c语言
- c++语言
- java语言
- Android学习
xml文件:
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 ArrayAdapteradapter = 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); adapter = ArrayAdapter.createFromResource(this,R.array.langurage,android.R.layout.simple_spinner_item); //设置下拉列表风格 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下拉列表效果”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。