安卓——Spinner(下拉列表)




activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:context="com.example.lenovo.spinner.MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:textColor="#ff0000"
        android:textSize="20sp"
        />
    <Spinner
        android:layout_marginTop="20dp"
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </Spinner>

</LinearLayout>

MainActivity.java

package com.example.lenovo.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{
    private TextView textView;
    private Spinner spinner;
    private List<String> data;
    private ArrayAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView) findViewById(R.id.textView);
        spinner=(Spinner)findViewById(R.id.spinner);
        data=new ArrayList<String>();
        textView.setText("您选择的城市是北京");
        //1、添加数据源,就是下拉菜单选项
        data.add("北京");
        data.add("深圳");
        data.add("上海");
        data.add("广州");
        //2、未下来列表定义一个数组适配器
        adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
        //3、为适配器设置下拉菜单的样式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //4、将适配器配置到下拉列表上
        spinner.setAdapter(adapter);
        //5、给下拉菜单设置监听事件
        spinner.setOnItemSelectedListener(this);
    }



    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String cityName=data.get(i);
        textView.setText("您选择的城市是"+cityName);
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}



Item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

    <ImageView
        android:id="@+id/image"
        android:background="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        />
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:textSize="50sp"
        android:layout_toRightOf="@id/image"
        android:text="None"
        />
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:context="com.example.lenovo.simpleadpaterofspinner.MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textColor="#ff0000"
        />

    <Spinner

        android:id="@+id/spinneer"

        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </Spinner>

</LinearLayout>

MainActivity.java

package com.example.lenovo.simpleadpaterofspinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

    private TextView textView;
    private Spinner spinner;
    private List<Map<String,Object>>data;
    private SimpleAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.text);
        spinner=(Spinner)findViewById(R.id.spinneer);
        //1.添加一个下拉列表的数据源
        data= new ArrayList<Map<String, Object>>();
        getdata();
        //2.为下拉列表定义一个适配器
        adapter=new SimpleAdapter(this,data,R.layout.item,new String[]{"image","text"},new int[]{R.id.image,R.id.textview});
        //3.为适配器设置下拉列表的样式
        adapter.setDropDownViewResource(R.layout.item);
        //4.给下拉列表添加适配器
        spinner.setAdapter(adapter);
        //5.为下来列表设置各种响应事件
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                textView.setText("您选择的是:"+adapter.getItem(i));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                textView.setText("None");
            }
        });

    }

    private void getdata(){
        Map<String,Object>map1=new HashMap<String,Object>();
        map1.put("imag",R.mipmap.ic_launcher);
        map1.put("text","北京");
        Map<String,Object>map2=new HashMap<String,Object>();
        map2.put("imag",R.mipmap.ic_launcher);
        map2.put("text","上海");
        Map<String,Object>map3=new HashMap<String,Object>();
        map3.put("imag",R.mipmap.ic_launcher);
        map3.put("text","深圳");
        Map<String,Object>map4=new HashMap<String,Object>();
        map4.put("imag",R.mipmap.ic_launcher);
        map4.put("text","广州");
        data.add(map1);
        data.add(map2);
        data.add(map3);
        data.add(map4);
    }
}

  • 8
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水之积也不厚,则其负大舟也无力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值