An example analysis of spinner usage of Android control _android

Source: Internet
Author: User
Tags gettext

This example describes the spinner usage of the Android control. Share to everyone for your reference. Specifically as follows:

Use of the following emulated Drop-down lists

Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:id=
"@+id/linearlayout01"
 android: Layout_width= "Fill_parent" android:layout_height= "fill_parent"
 android:orientation= "vertical"
 xmlns: Android= "Http://schemas.android.com/apk/res/android" >
 <textview android:text= "@string/ys" 
 Android : id= "@+id/textview01"
 android:layout_width= "fill_parent"
 android:layout_height= "Wrap_content" 
 Android:textsize= "28dip"/>
 <spinner android:id= "@+id/spinner01" android:layout_width= "Fill_"
 Parent "
 android:layout_height=" wrap_content "/>
</LinearLayout>

Spinneractivity class:

Package com.ljq.sp;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AdapterView;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
Import Android.widget.Spinner;
Import Android.widget.TextView;
Import Android.widget.AdapterView.OnItemSelectedListener;
 public class Spinneractivity extends activity {private Spinner SP = null;//dropdown list private TextView TV = null;
 Array of all resource pictures private int[] Drawableids={r.drawable.football,r.drawable.basketball,r.drawable.volleyball};
 Array of all strings private int[] MSGIDS={R.STRING.ZQ,R.STRING.LQ,R.STRING.PQ};
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 Setcontentview (R.layout.main);
 TV = (TextView) Findviewbyid (R.ID.TEXTVIEW01);
 sp= (Spinner) This.findviewbyid (R.ID.SPINNER01);//Initialize Spinner Sp.setadapter (adapter); Sp.setonitemselectedlistEner (New Onitemselectedlistener () {public void onitemselected (adapterview<?> parent, view view, int positon, long
  ID) {LinearLayout ll = (linearlayout) view;
  View v=ll.getchildat (0);//Get First control ImageView log.i ("Ljq", V.getclass (). GetName ());
  TextView TVN = (TextView) ll.getchildat (1);//Get second control TextView StringBuilder sb = new StringBuilder ();
  Sb.append (Getresources (). GetText (R.string.ys)). Append (":"). Append (Tvn.gettext ());
  Tv.settext (Sb.tostring ());
 public void onnothingselected (adapterview<?> parent) {}});
 Private Baseadapter adapter = new Baseadapter () {public int getcount () {return drawableids.length;
 Public Object getitem (int position) {return drawableids[position];
 public long getitemid (int position) {return position; Public View GetView (int position, View Convertview, ViewGroup parent) {LinearLayout LL = new LinearLayout (spinneract
  Ivity.this);
  Ll.setorientation (linearlayout.horizontal); ImageView IV = new ImagevIew (Spinneractivity.this);
  Iv.setimageresource (Drawableids[position]);
  Ll.addview (iv);
  TextView tv=new TextView (spinneractivity.this);
  Tv.settext (Msgids[position]);/set content tv.settextsize (24);
  Tv.settextcolor (R.color.black);
  Ll.addview (TV);
 return ll;
}
 };

 }

Run results

I hope this article will help you with your Android order design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.