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/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" android: paddingBottom = "@ dimen/plugin" 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>