翻翻git之---偏向iOS風格的自訂控制項 ToggleSwitch

來源:互聯網
上載者:User

翻翻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

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.