zoukankan      html  css  js  c++  java
  • Spinner

    <resources>
        <string name="app_name">HelloWorld</string>
    
        <string-array name="city_labels">
            <item>北京</item>
            <item>上海</item>
            <item>广州</item>
            <item>深圳</item>
        </string-array>
    </resources>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical"
        tools:context="com.pingyijinren.helloworld.MainActivity">
    
        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/city_labels"
            android:id="@+id/spinner"></Spinner>
    </LinearLayout>
    package com.pingyijinren.helloworld;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Spinner;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
        private Spinner spinner;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            spinner=(Spinner)findViewById(R.id.spinner);
            spinner.setPrompt("选择你所在的城市");
            spinner.setOnItemSelectedListener(this);
        }
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String[] cities=getResources().getStringArray(R.array.city_labels);
            Toast.makeText(this,cities[position]+"",Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    }
  • 相关阅读:
    理解volatile与synchronized
    实现任意两个数的互换
    增删改查+部分前段内容
    Hibernate知识点小结汇总
    Spring知识点小结(一)
    JDBC相关
    Redis工具之Jedis
    Hibernate知识点小结(四)--JPA
    Hibernate知识点小结(三)-->一对多与多对多配置
    Hibernate知识点小结(二)
  • 原文地址:https://www.cnblogs.com/zqxLonely/p/5552359.html
Copyright © 2011-2022 走看看