Android-Spinner [implemented using C # And Java],

Source: Internet
Author: User

Android-Spinner [implemented using C # And Java],
The effect is as follows:

C # implementation code
Using Android. app; using Android. OS; using Android. widget; namespace SpinnerDemo {[Activity (Label = "@ string/ApplicationName", MainLauncher = true, Icon = "@ drawable/icon")] public class MainActivity: activity {private Spinner _ citySpinner; private TextView _ cityNameView; private ArrayAdapter <string> _ cityInfos; protected override void OnCreate (Bundle bundle) {base. onCreate (bundle); SetContentView (Resource. layout. main); _ citySpinner = FindViewById <Spinner> (Resource. id. sp_city); _ cityNameView = FindViewById <TextView> (Resource.Id.txt _ cityName); _ cityInfos = new ArrayAdapter <string> (this, Android. resource. layout. simpleSpinnerDropDownItem); _ cityInfos. add ("Chengdu"); _ cityInfos. add ("Lanzhou"); _ cityInfos. add ("Wuhan"); _ cityInfos. add ("Shanghai"); _ citySpinner. adapter = _ cityInfos; _ citySpinner. itemSelected + = CitySelectedEvent;} private void CitySelectedEvent (object sender, AdapterView. itemSelectedEventArgs e) {_ cityNameView. text = _ cityInfos. getItem (e. position );}}}
Java implementation code
Package com. example. halower. spinnerdemo; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. view; import android. widget. adapterView; import android. widget. arrayAdapter; import android. widget. spinner; import android. widget. textView; public class MainActivity extends ActionBarActivity {private Spinner _ citySpinner; private TextView _ cityNameView; private ArrayAdap Ter <String> _ cityInfo; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); _ citySpinner = (Spinner) findViewById (R. id. sp_city); _ cityNameView = (TextView) findViewById(R.id.txt _ cityName); _ cityInfo = new ArrayAdapter <> (this, R. layout. support_simple_spinner_dropdown_item); _ cityInfo. add ("Chengdu"); _ cityInfo. add ("Lanzhou"); _ ci TyInfo. add ("Wuhan"); _ cityInfo. add ("Shanghai"); _ citySpinner. setAdapter (_ cityInfo); _ citySpinner. setOnItemSelectedListener (new AdapterView. onItemSelectedListener () {@ Override public void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {_ cityNameView. setText (_ cityInfo. getItem (position) ;}@ Override public void onNothingSelected (AdapterView <?> Parent ){}});}}
LayOut
<RelativeLayout 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: paddingLeft =" @ dimen/plugin "android: paddingRight =" @ dimen/activity_horizontal_margin "android: paddingTop = "@ dimen/activity_vertical_margin" android: paddingBottom = "@ dimen/activity_vertical_margin" tools: context = ". mainActivity "> <Spinner android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: id =" @ + id/sp_city "android: layout_alignParentTop =" true "android: layout_alignParentRight = "true" android: Rule = "true" android: layout_toEndOf = "@ + id/txt_cityName" android: layout_toRightOf = "@ + id/txt_cityName"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "city" android: id = "@ + id/txt_cityName" android: layout_alignParentLeft = "true" android: layout_alignParentStart = "true"/> </RelativeLayout>

 

Related Article

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.