当前位置: 移动技术网 > IT编程>移动开发>Android > 安卓开发笔记(二十八):仿写IOS switch选择器控件实现,checkbox

安卓开发笔记(二十八):仿写IOS switch选择器控件实现,checkbox

2019年04月26日  | 移动技术网IT编程  | 我要评论

村中猛男,生活大爆炸第九季下载,古诗集

我们先来看看效果:

这里我们主要使用了github上的一个开源项目,配置起来比较方便,下面解释一下该如何使用:
首先是:
gradle文件当中进行配置:

dependencies {
  
    implementation 'ch.ielse:switchbutton:1.0.1'
   
}

当然如果是老版本的android studio则使用:

dependencies {
  
    complie 'ch.ielse:switchbutton:1.0.1'
   
}

 

二.activity

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.widget.toast;

import ch.ielse.view.switchview;

public class primarycolor extends appcompatactivity {

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.primary_color);
        switchview switchview=(switchview)findviewbyid(r.id.button);
        switchview.setonstatechangedlistener(new switchview.onstatechangedlistener() {
            @override
            public void toggletoon(switchview view) {
                view.toggleswitch(true); // or false
                toast.maketext(primarycolor.this,"您触发了这个事件",toast.length_short).show();
            }

            @override
            public void toggletooff(switchview view) {
                view.toggleswitch(false); // or true
            }
        });


    }
}

其中的第一个监听器是switch在选择的时候,所触发的事件。第二个监听器是未在选择的时候,所触发的事件。这样使用起来比较方便。

 三.actvity.xml

<?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"

    tools:context=".primarycolor">
    <ch.ielse.view.switchview
        android:layout_margin="150dp"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</linearlayout>

得解,一共就只有这三个部分了,很容易的,如果自己去写,不调用开源库的话将会浪费很多时间。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网