Android -- 开源库NiceSpinner 的基本使用
Posted Kevin-Dev
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android -- 开源库NiceSpinner 的基本使用相关的知识,希望对你有一定的参考价值。
不断学习,做更好的自己!💪
视频号 | CSDN | 简书 |
---|---|---|
欢迎打开微信,关注我的视频号:程序员朵朵 | 点我 | 点我 |
如今,好多 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. 布局文件中使用:
<?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>
3. java 代码中使用:
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;
);
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;
<?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>
以上是关于Android -- 开源库NiceSpinner 的基本使用的主要内容,如果未能解决你的问题,请参考以下文章
android 多级下拉菜单实现教程 NiceSpinner 文字展示不全 NiceSpinner demo NiceSpinner加图标 可以在下拉框的选项(item)中加图片:
android 多级下拉菜单实现教程 NiceSpinner 文字展示不全 NiceSpinner demo NiceSpinner加图标 可以在下拉框的选项(item)中加图片: