安卓开发笔记(二十八):仿写IOS switch选择器控件实现,checkbox
2023-09-11 14:21:08 时间
我们先来看看效果:
这里我们主要使用了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>
得解,一共就只有这三个部分了,很容易的,如果自己去写,不调用开源库的话将会浪费很多时间。
相关文章
- iOS开发系列课程预告
- iOS:实现表格填充和选择操作
- JS判断当前页面是在 QQ客户端/微信客户端/iOS浏览器/Android浏览器/PC客户端
- IOS 7 Study - Implementing Navigation with UINavigationController
- 《iOS和tvOS 2D游戏开发教程》——第2章,第2.1节Sprite Kit游戏循环
- 《iOS 8应用开发入门经典(第6版)》——第2章,第2.4节小结
- 《iOS 8案例开发大全》——实例006 实现复杂的查找和替代工作
- ios数组倒序
- iOS常用宏 定义
- Hello_IOS ios开发transform属性
- iOS UIView非常用方法及属性详解
- SwiftUI 组件大全之 iOS 15 循环视频背景组件 AVKit 和 AVFoundation
- 分分钟解决iOS开发中App启动广告的功能
- iOS教你轻松打造瀑布流Layout
- Delphi xe7 FireMonkey / Mobile (Android, iOS)生成 QR Code完整实例
- ios 移动应用通用逻辑流程
- weex-platform里iOS项目打包报错/platforms/ios/../../start does not exist.
- iOS - 高德地图步行线路规划多点多条线路
- IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
- 黑莓允许开发者将通讯软件BBM植入iOS和Android应用
- 苹果宣布创立欧洲首个iOS开发中心