Android學習筆記之控制項Spinner

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

1.mian.xml布局檔案

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6      7     <TextView  8         android:id="@+id/textView1" 9         android:layout_width="match_parent"10         android:layout_height="wrap_content"11         android:text="你輸入的是阿森納"12         android:textSize="20sp"/>13     14     <Spinner 15         android:id="@+id/spinner1"16         android:layout_marginTop="10dp"17         android:layout_width="match_parent"18         android:layout_height="wrap_content"/>19     20 21 </LinearLayout>

 

2.item.xml樣式布局檔案

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="horizontal" > 6      7     <ImageView  8         android:id="@+id/imageView1" 9         android:layout_width="60dp"10         android:layout_height="60dp"11         android:src="@drawable/a"/>12     13     <TextView 14         android:id="@+id/textView2"15         android:layout_width="match_parent"16         android:layout_height="60dp"17         android:text="阿森納"18         android:textSize="20sp"19         android:gravity="center"/>20     21 22 </LinearLayout>

 

3.java代碼

 

 1 public class MainActivity extends Activity  { 2      3     private TextView tv1; 4     private Spinner s1; 5     private List<Map<String,Object>> dataList; 6     private SimpleAdapter sa; 7     private int[] pic = {R.drawable.a,R.drawable.l,R.drawable.m,R.drawable.q}; 8     private String[] name = {"阿森納","利物浦","曼聯","切爾西"}; 9 10     @Override11     protected void onCreate(Bundle savedInstanceState) {12         super.onCreate(savedInstanceState);13         setContentView(R.layout.main);14         15         tv1 = (TextView) findViewById(R.id.textView1);16         s1 = (Spinner) findViewById(R.id.spinner1);17         18         dataList = new ArrayList<Map<String,Object>>();19         20         //建立適配器21         sa = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic","name"}, new int[]{R.id.imageView1,R.id.textView2});22         23         //設定一個下拉的樣式表24         sa.setDropDownViewResource(R.layout.item);25         s1.setAdapter(sa);26         27         //s1設定監聽器28         s1.setOnItemSelectedListener(new OnItemSelectedListener() {29 30             @Override31             public void onItemSelected(AdapterView<?> parent, View view,32                     int position, long id) {33                 // TODO Auto-generated method stub34                 String team = sa.getItem(position).toString();35                 tv1.setText("你輸入的是"+team);36             }37 38             @Override39             public void onNothingSelected(AdapterView<?> parent) {40                 // TODO Auto-generated method stub41                 42             }43         });44     }45     //設定資料來源46     private List<Map<String,Object>> getData(){47         for(int i = 0;i<pic.length;i++){48             Map<String, Object> map = new HashMap<String, Object>();49             map.put("pic",pic[i]);50             map.put("name", name[i]);51             dataList.add(map);52         }53         return dataList;54     }55     56 }

 

Android學習筆記之控制項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.