安卓——Spinner(下拉列表)

  • Post author:
  • Post category:其他





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);
    }
}



版权声明:本文为weixin_38199770原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。