您当前的位置: 首页 >  android

【Android -- 开源库】NiceSpinner 的基本使用

发布时间:2017-12-28 15:33:42 ,浏览量:5

在这里插入图片描述

如今,好多 App 都有下拉选择框的需求,今天这个第三方下拉框可以满足我们大部分的需求噢! GitHub 地址: nice-spinner

效果图

这里写图片描述

使用

1. 在 build.gradle 文件添加:

allprojects { repositories { ... maven { url "https://jitpack.io" } } } dependencies { ... compile 'com.github.arcadefire:nice-spinner:1.3.1' ... } 

2. 布局文件中使用:

 <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"/>  @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; } } }); } } 

4. ToastUtil.java

/**
 * 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(); } } 

5. MyApplication.java

/**
 * 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; } } 
 <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" />             
关注
打赏
1688896170
查看更多评论

暂无认证

  • 5浏览

    0关注

    109478博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0701s