Android 中TeaPickerView數(shù)據(jù)級聯(lián)選擇器功能的實例代碼
Github地址
(Github排版比較好,建議進入這里查看詳情,如果覺得好,點個star吧!)

引入module
allprojects {
repositories {
google()
jcenter()
maven { url 'https://www.jitpack.io' }
}
}
implementation 'com.github.YangsBryant:TeaPickerView:1.0.2'
主要代碼
public class MainActivity extends AppCompatActivity {
@BindView(R.id.mButton)
Button button;
List<String> mProvinceDatas=new ArrayList<>();
Map<String, List<String>> mSecondDatas= new HashMap<>();
Map<String, List<String>> mThirdDatas= new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind( this );
intiPickerView();
}
private void intiPickerView(){
//一級列表
ProvinceBean provinceBean = new ProvinceBean();
mProvinceDatas.addAll(provinceBean.getRepData().getProvince());
//二級列表
SecondBean secondBean = new SecondBean();
mSecondDatas.putAll(secondBean.getRepData().getSecond());
//三級列表
ThirdBean thirdBean = new ThirdBean();
mThirdDatas.putAll(thirdBean.getRepData().getThird());
Log.i("json", JsonArrayUtil.toJson(mProvinceDatas));
Log.i("json",JsonArrayUtil.toJson(mSecondDatas));
Log.i("json",JsonArrayUtil.toJson(mThirdDatas));
//設(shè)置數(shù)據(jù)有多少層級
PickerData data=new PickerData();
data.setFirstDatas(mProvinceDatas);//json: ["廣東","江西"]
data.setSecondDatas(mSecondDatas);//json: {"江西":["南昌","贛州"],"廣東":["廣州","深圳","佛山","東莞"]}
data.setThirdDatas(mThirdDatas);//json: {"廣州":["天河區(qū)","白云區(qū)","番禹區(qū)","花都區(qū)"],"贛州":["章貢區(qū)","黃金開發(fā)區(qū)"],"東莞":["東城","南城"],"深圳":["南山區(qū)","寶安區(qū)","龍華區(qū)"],"佛山":["禪城區(qū)","順德區(qū)"],"南昌":["東湖區(qū)","青云譜區(qū)","青山湖區(qū)"]}
data.setInitSelectText("請選擇");
TeaPickerView teaPickerView =new TeaPickerView(this,data);
teaPickerView.setScreenH(3)
.setDiscolourHook(true)
.setRadius(25)
.setContentLine(true)
.setRadius(25)
.build();
button.setOnClickListener(v -> {
//顯示選擇器
teaPickerView.show(button);
});
//選擇器點擊事件
teaPickerView.setOnPickerClickListener(pickerData -> {
Toast.makeText(MainActivity.this,pickerData.getFirstText()+","+pickerData.getSecondText()+","+pickerData.getThirdText(),Toast.LENGTH_SHORT).show();
teaPickerView.dismiss();//關(guān)閉選擇器
});
}
}
TeaPickerView屬性大全
|
方法名 |
屬性 |
|
setHeights(int mHeight) |
顯示具體的高度(dp),設(shè)置0是自適應(yīng)(高度沒有默認值,需要主動設(shè)置) |
| setScreenH(int num) | 顯示的高度占屏幕的百分比 |
| setBackground(int color) | 設(shè)置整體的背景顏色 默認是#ffffff |
| setRadius(int mRadius) | 設(shè)置圓角,默認0 |
| setContentBackground(int color) | 內(nèi)容欄的背景顏色 默認是#ffffff |
| setContentHeight(int mHeight) | 內(nèi)容欄的高度(dp) 默認是50dp |
| setContentText(int size,int color) | 內(nèi)容欄字體的大小和顏色, 默認是16sp,#0aa666,用此方法會固定顏色 |
| setContentText(ColorStateList drawable) | 自定義內(nèi)容欄字體顏色變換器 在res目錄下創(chuàng)建color文件夾用selector 默認顏色#555 選中顏色#0aa666 |
| setContentLine(boolean bl) | 內(nèi)容欄選中是否有下劃線 默認不開啟 |
| setContentLineColor(Drawable drawable) | 自定義內(nèi)容欄下劃線用layer-list 默認是下邊框描邊 顏色#0fbc72 高度1dp |
| setLine(int mHeight,int color) | 分割線的高度和顏色 默認是0.5dp #e5e5e5 |
| setitemHeight(int mHeight) | 設(shè)置list的item的高度(dp) 默認是40dp |
| setListText(int size,int color) | 設(shè)置list的字體大小和顏色 默認是15 #555 |
| setScrollBal(boolean bl) | 設(shè)置list是否顯示滾動條,默認false |
| setAlpha(float mFloat) | 設(shè)置陰影層的透明度 默認是0.5f |
| setDiscolour(boolean bl) | 設(shè)置選中項是否加色,默認true |
| setDiscolourColor(int color) | 設(shè)置選中項加色的顏色值,默認#0aa666 |
| setDiscolourHook(boolean bl) | 設(shè)置選中項是否有√圖標,默認false |
| setCustomHook(Drawable drawable) | 自定義√圖標 |
| build() | 參數(shù)設(shè)置完畢,一定要build |
設(shè)置數(shù)據(jù)
| 方法名 | 屬性 |
| setInitSelectText(String firstText) | 初始文字 |
| setFirstDatas(List mFirstDatas) | 設(shè)置一級數(shù)據(jù) |
| setSecondDatas(Map<String, List> mSecondDatas) | 設(shè)置二級數(shù)據(jù) |
| setThirdDatas(Map<String, List> mThirdDatas) | 設(shè)置三級數(shù)據(jù) |
| setFourthDatas(Map<String, List> mFourthDatas) |
給出參考bean地址
默認內(nèi)容欄字體顏色變換器
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:color="@color/picker_select_text_color"/> <item android:state_pressed="true" android:color="@color/picker_select_text_color"/> <item android:state_checked="true" android:color="@color/picker_select_text_color"/> <item android:state_focused="true" android:color="@color/picker_select_text_color"/> <item android:color="@color/picker_text_color"/> </selector>
默認內(nèi)容欄下劃線
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 邊框顏色值 -->
<item>
<shape>
<solid android:color="@color/station_average" />
</shape>
</item>
<item android:bottom="1dp"> <!--設(shè)置只有底部有邊框-->
<shape>
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
總結(jié)
以上所述是小編給大家介紹的Android 中TeaPickerView數(shù)據(jù)級聯(lián)選擇器功能的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
android monkey自動化測試改為java調(diào)用monkeyrunner Api
一般情況下我們使用android中的monkeyrunner進行自動化測試時,使用的是python語言來寫測試腳本。不過,最近發(fā)現(xiàn)可以用java調(diào)用monkeyrunner Api,于是,就簡單研究了一下。這里做一些總結(jié)。希望有對在研究的午飯可以有所用處2012-11-11
TextView顯示系統(tǒng)時間(時鐘功能帶秒針變化
用System.currentTimeMillis()可以獲取系統(tǒng)當前的時間,我們可以開啟一個線程,然后通過handler發(fā)消息,來實時的更新TextView上顯示的系統(tǒng)時間,可以做一個時鐘的功能2013-11-11
Android使用SharedPreferences存儲數(shù)據(jù)的實現(xiàn)方法
這篇文章主要介紹了Android使用SharedPreferences存儲數(shù)據(jù)的實現(xiàn)方法,可實現(xiàn)針對短信的臨時保存功能,非常簡單實用,需要的朋友可以參考下2016-06-06
Android自定義View 實現(xiàn)鬧鐘喚起播放鬧鐘鈴聲功能
這篇文章主要介紹了Android自定義View 實現(xiàn)鬧鐘喚起播放鬧鐘鈴聲的效果,本文通過實例代碼給大家詳解,需要的朋友可以參考下2016-12-12
Android編程實現(xiàn)畫板功能的方法總結(jié)【附源碼下載】
這篇文章主要介紹了Android編程實現(xiàn)畫板功能的方法,結(jié)合實例形式總結(jié)分析了Android基于自定義View與Canvas類實現(xiàn)畫板功能的具體操作步驟與相關(guān)注意事項,需要的朋友可以參考下2018-02-02
Android開發(fā)應(yīng)用中Broadcast Receiver組件詳解
本篇文章主要介紹了Android開發(fā)應(yīng)用中Broadcast Receiver組件詳解,想要學習的同學可以了解一下。2016-11-11

