您好,登錄后才能下訂單哦!
這篇文章主要介紹“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。