Bind a spinner in the activity

Source: Internet
Author: User

(1) A major layout file activity_main is required.

<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"    tools:context="${packageName}.${activityClass}" >    <TextView        android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignRight="@id/title"        android:text="Spinner" />    <Spinner        android:id="@+id/city"        android:layout_width="82dp"        android:layout_height="30dp"        android:layout_alignParentLeft="true"        android:layout_below="@+id/title"        android:layout_marginTop="18dp"        android:layout_weight="0.15" /></RelativeLayout>

(2) code implementation
 MainActivity.java

package com.liuzuyi.sp;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.ArrayAdapter;import android.widget.Spinner;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Spinner spin=(Spinner)findViewById(R.id.city);    ArrayAdapter<CharSequence> adapter =  ArrayAdapter.createFromResource(this, R.array.city, android.R.layout.simple_spinner_item);         adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);         spin.setAdapter(adapter); }}
This is different from implementing the spinner binding in the fragement framework, which will be explained in my next 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.