android spinner 简书,安卓之Spinner

package com.example.spinner;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//准备数据源

final String string[] ={"北京","上海","广州"};

//准备适配器;

ArrayAdapter adapter = new ArrayAdapter(

MainActivity.this,

R.layout.item,

R.id.text,

string);

//3.让控件和适配器进行关联;

Spinner spinner = findViewById(R.id.spinner);

spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override

public void onItemSelected(AdapterView> adapterView, View view, int i, long l) {

Toast.makeText(MainActivity.this, string[i], Toast.LENGTH_SHORT).show();

}

@Override

public void onNothingSelected(AdapterView> adapterView) {

}

});

}

}

activity_main.xml:

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding= "20dp"

tools:context="com.example.spinner.MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请选择城市!"

/>

android:id= "@+id/spinner"

android:layout_width="match_parent"

android:layout_height="wrap_content">

item.xml:

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:src="@mipmap/ic_launcher"

android:layout_width="30dp"

android:layout_height="30dp" />

android:id="@+id/text"

android:textSize="20sp"

android:text="helloworld"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值