Android使用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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:background="@color/colorbackground">
        <Spinner
            android:id="@+id/spinner_test_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:src="@drawable/next"
            />
        <Spinner
            android:id="@+id/spinner_test_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:src="@drawable/next"
            />
    </LinearLayout>

</LinearLayout>

第二步,添加(Butterknife引用),也可以不用

修改工程的build.gradle:

buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://www.jitpack.io' }
    }
}

修改项目下的build.gradle:

 implementation 'com.jakewharton:butterknife:8.8.1'
 annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

第三步,修改MainActivity.java

package com.example.studyproject;

import android.content.Context;
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;

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

import butterknife.BindViews;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
  @BindViews({R.id.spinner_test_one,R.id.spinner_test_two})
    List<Spinner> spinnerList;
    String item_name;
    Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
      ButterKnife.bind(this);
      initView();
  }

    private void initView() {
        final List<String> item = new ArrayList<>();
        item.add("头条"); //0
        item.add("社会");
        item.add("国际");//2
        item.add("国内");
        item.add("体育");
        item.add("娱乐");
        ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext,R.layout.simple_spinner_item,item);
       adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);  //选中的样式
        spinnerList.get(0).setAdapter(adapter);
        //这里别选成了ItemClickListener
        spinnerList.get(0).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int arg, long l) {
                   if (arg ==0){
                       item_name = "头条";
                   }else if (arg ==1){
                       item_name = "社会";
                       
                   }else if (arg ==2){
                       item_name = "国际";
                   }
                   else if (arg ==3){
                       item_name = "国内";
                   }
                Toast.makeText(mContext,"你点击了"+item_name,Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}

下拉的样式:

<?xml version="1.0" encoding="utf-8"?><!--
/* //device/apps/common/assets/res/any/layout/simple_spinner_item.xml
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" />

这篇对于spinner的使用不多,只是基本使用,后续将会补上当第一个下拉后,第二个会对应刷新数据下拉。
第二篇:Android使用Spinner实现下拉选功能(获取远程数据并根据下拉刷新) (学习笔记 (二))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android使用Spinner控件实现城市级联下拉框,可以通过以下步骤实现: 1. 在布局文件中添加两个Spinner控件,分别用于显示省份和城市列表。 ``` <Spinner android:id="@+id/province_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Spinner android:id="@+id/city_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 2. 创建两个数组,一个用于存储省份列表,另一个用于存储城市列表。 ``` String[] provinces = {"北京", "上海", "广东省", "山东省", "浙江省"}; String[][] cities = { {"北京市"}, {"上海市"}, {"广州市", "深圳市", "珠海市", "汕头市", "韶关市"}, {"济南市", "青岛市", "淄博市", "枣庄市", "东营市"}, {"杭州市", "宁波市", "温州市", "嘉兴市", "湖州市"} }; ``` 3. 将省份列表添加到省份Spinner控件中。 ``` Spinner provinceSpinner = findViewById(R.id.province_spinner); ArrayAdapter<String> provinceAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, provinces); provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); provinceSpinner.setAdapter(provinceAdapter); ``` 4. 根据省份择,动态生成对应的城市列表,并添加到城市Spinner控件中。 ``` Spinner citySpinner = findViewById(R.id.city_spinner); provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { ArrayAdapter<String> cityAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, cities[position]); cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); citySpinner.setAdapter(cityAdapter); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); ``` 这样就可以实现Android城市下拉列表,使用Spinner控件实现城市级联下拉框的效果了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雪の星空朝酱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值