翻翻git之---偏向iOS風格的自訂控制項 ToggleSwitch
這一篇上一個自訂控制項ToggleSwitch
:
第一眼看上去感覺就像舊版的iOS風格的Switch。
How to use?
Grade:
dependencies { compile 'us.belka:androidtoggleswitch:1.1.1'}
Maven:
us.belka androidtoggleswitch 1.0 pom
Eclipse:
Copy圈出來的部分就好了
如何設定?(這邊拿Eclipse Copy完為例)
中第2個效果就是 這樣的 如果要3個那就再加一個 custom:textToggleCenter="預設" 第一個效果就是添加後的效果
那如果你又許多個?不止三個?
那就在業務的Activity中擷取控制項對象,再傳入一個 ArrayList就行了
像這樣
ToggleSwitch toggleSwitch = (ToggleSwitch) findViewById(R.id.multiple_switches);ArrayList labels = new ArrayList<>();labels.add("AND");labels.add("OR");labels.add("XOR");labels.add("NOT");labels.add("OFF");toggleSwitch.setLabels(labels);
效果如下:
因為他不是繼承於Switch所以,也就沒有所謂的true false2個傳回值的概念,全部都由 position返回,也就是從最左邊->最右邊,從0開始遞增就是所被點擊的那個position
那麼如何擷取position呢?
aTo = (ToggleSwitch) findViewById(R.id.aTo); aTo.setOnToggleSwitchChangeListener(new ToggleSwitch.OnToggleSwitchChangeListener() { @Override public void onToggleSwitchChangeListener(int position) { Toast.makeText(MainActivity.this,position+"被點擊",Toast.LENGTH_SHORT).show(); } });
例子中的土司也就是這麼實現的。
那如果你要初始化設定某個為預設項,就設定下position 像這樣
toggleSwitch.setCheckedTogglePosition(position);
要擷取就這樣
int position = toggleSwitch.getCheckedTogglePosition();
還有些設定內容下面也羅列一下,主要是字型大小啊,顏色啊,間距什麼的
Option Name |
Format |
Description |
android:textSize |
dimension |
Text size of each button |
custom:activeBgColor |
color |
Background color of the checked button |
custom:activeTextColor |
color |
Text color of the checked button |
custom:inactiveBgColor |
color |
Background color of the inactive buttons |
custom:inactiveTextColor |
color |
Text color of the inactive buttons |
custom:separatorColor |
color |
Color of the vertical separator between inactive buttons |
custom:toggleWidth |
dimension |
Width of each button |
總體使用起來和源生控制項一樣,沒什麼區別,實現難度也不高,大家可以適當的學習下,自己頁寫寫 會有長進。
源碼地址:https://github.com/BelkaLab/Android-Toggle-Switch/archive/master.zip