Android 下拉选项框用 NiceSpinner实现

首先先看一下效果图

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

1,在 build.gradle 文件添加:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

dependencies {
    ...
    compile 'com.github.arcadefire:nice-spinner:1.3.1'
    ...
}

2,布局文件中使用:

<?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.gyq.nicespinner.MainActivity">

    <org.angmarch.views.NiceSpinner
        android:id="@+id/nice_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:arrowTint="@color/colorPrimary"
        app:textTint="@color/colorAccent"/>


</LinearLayout>

2,java 代码中使用:

package com.gyq.nicespinner;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;

import com.gyq.nicespinner.utils.ToastUtil;

import org.angmarch.views.NiceSpinner;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NiceSpinner niceSpinner = (NiceSpinner)findViewById(R.id.nice_spinner);
        List<String> dataList = new ArrayList<>();
        dataList.add("android");
        dataList.add("java");
        dataList.add("ios");
        dataList.add("php");
        dataList.add("kotlin");

        niceSpinner.attachDataSource(dataList);

        niceSpinner.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0 :
                        ToastUtil.showToast("android");
                        break;
                    case 1 :
                        ToastUtil.showToast("java");
                        break;
                    case 2 :
                        ToastUtil.showToast("ios");
                        break;
                    case 3 :
                        ToastUtil.showToast("php");
                        break;
                    case 4 :
                        ToastUtil.showToast("kotlin");
                        break;
                }
            }
        });
    }
}

3,ToastUtil.java

package com.gyq.nicespinner.utils;

import android.os.Looper;
import android.widget.Toast;

import com.gyq.nicespinner.base.MyApplication;

/**
 * Created by gyq on 2017/12/28 11:54
 */

public class ToastUtil {

        public static void showToast(String msg){
            if(isMainThread()){
                Toast.makeText(MyApplication.getContext(),msg,Toast.LENGTH_SHORT).show();
            }else{
                Looper.prepare();
                Toast.makeText(MyApplication.getContext(),msg,Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }

        public static void showLongToast(String msg){
            if(isMainThread()){
                Toast.makeText(MyApplication.getContext(),msg,Toast.LENGTH_LONG).show();
            }else{
                Looper.prepare();
                Toast.makeText(MyApplication.getContext(),msg,Toast.LENGTH_LONG).show();
                Looper.loop();
            }
        }

        public static boolean isMainThread() {
            return Looper.getMainLooper().getThread() == Thread.currentThread();
        }

}

4,MyApplication.java

package com.gyq.nicespinner.base;

import android.app.Application;
import android.content.Context;

/**
 * Created by gyq on 2017/12/28 11:54
 */

public class MyApplication extends Application {
    private static Context appContext;

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = this.getApplicationContext();
    }

    public static Context getContext() {
        return appContext;
    }
}

5.最后在AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gyq.nicespinner">

    <application
        **android:name=".base.MyApplication"**
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果想实现listview那一块用循环遍历然后进行add应该就可以了,个人理解,以后可能会修改这个博客,还是初学者。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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控件实现城市级联下框的效果了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值