android之ArrayAdaper之Spinner

來源:互聯網
上載者:User

標籤:android   layout   

listView頁面配置:layout/activity_main.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">


    <TableLayout 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:stretchColumns="1">
       <TableRow >
           <TextView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="選擇年份"
               android:layout_gravity="center_vertical"/>
           <Spinner 
               android:id="@+id/spYears"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:prompt="@string/prompt_year"//1、設定預設的顯示,2、prompt  [prm(p)t] 【電腦】提示資訊的
               android:spinnerMode="dialog"//設定下拉的樣式
               />
       </TableRow> 
        
    </TableLayout>


</LinearLayout>

該布局下的java代碼:

package com.example.day05_0222;


import android.support.v7.app.ActionBarActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
private Spinner spYear;//下來選框的對象
private BaseAdapter arrayAdapter;//適配器
private String [] spyearnames;//資料數組
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initData();
setListener();
}
private void initData() {//初始化資料
// TODO Auto-generated method stub
   Resources resource = getResources();
   spyearnames =  resource.getStringArray(R.array.years);
}
private void setListener() {//設定選中後的監聽事件
// TODO Auto-generated method stub
spYear.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
switch(parent.getId()){
case R.id.spYears:
String year = spyearnames[position];
Toast.makeText(MainActivity.this, "選中的年份是:"+year, 20000).show();
break;
}
}


@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}

});
}
private void init() {
// TODO Auto-generated method stub
//得到下來選框的對象
spYear = (Spinner) findViewById(R.id.spYears);
//得到配接器物件

//重點解釋第一個參數是要顯示的布局所在的activity對象,第二個參數是指要顯示的集合資料的物理地址,第三個參數是

                // 指適配的布局頁面,這裡直接用了android內建的布局
arrayAdapter = ArrayAdapter.createFromResource(this, R.array.years, android.R.layout.simple_list_item_1);
//設定適配器
spYear.setAdapter(arrayAdapter);
}



}

String字串資源:values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">Day05_0222</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
<string name="prompt_year">選擇年份</string>
<string-array name="years">
   <item>2000</item>
   <item>2001</item>
   <item>2002</item>
   <item>2003</item>
   <item>2004</item>
   <item>2005</item>
   <item>2006</item>
   <item>2007</item>
   
</string-array>
</resources>

效果:

650) this.width=650;" src="http://img.blog.csdn.net/20141130103951394" />

本文出自 “追隨心的彼岸” 部落格,謝絕轉載!

android之ArrayAdaper之Spinner

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.