- 界面间传递对象 :https://www.jianshu.com/p/5106b0919d2d
- 界面跳转:
startActivity(Intent(context, StudentDeleteClassActivity::class.java).apply {
putExtra("schedule", schedule)
})
- 非空判断+foreach+when语法:
teacher?.let {
tv_name.text = it.nickname
it.sparringYear?.let { year ->
tv_tag_1.text = "教龄${year}年"
}
it.title.split("|").forEachWithIndex { i, s ->
when (i) {
0 -> tv_tag_2.let { tv ->
tv.visibility = View.VISIBLE
tv.text = s
}
1 -> tv_tag_3.let { tv ->
tv.visibility = View.VISIBLE
tv.text = s
}
}
}
}
- tv文字引入多个变量+变量变色
helper?.setText(R.id.tv_des, SpannableStringBuilder().apply {
arrayListOf("剩余${availableCount}次 ", "已排${scheduledCount}次 ", "已上${consumeCount}次 ").forEach {
this.append(SpannableStringBuilder(it).apply {
this.setSpan(ForegroundColorSpan(ContextCompat.getColor(context!!, R.color.round_btn_text2)), 2, it.length - 2, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
})
}
})
- 设置布局
override fun attachLayoutRes(): Int = R.layout.activity_student_class_table
- 多方法回调函数,同Java
var timeTableListener: TimeTableListener? = null
interface TimeTableListener {
fun onDefaultClick(date: Date)
fun onAddClick(date: Date)
fun onDisableClick(date: Date)
fun onHavaTeacherSelect(date: Date)
fun onConFlictSelect(date: Date)
fun onMissTeacherSelect(date: Date)
fun onHavaTeacherLongClick(date: Date)
}
//设置监听
time_table.timeTableListener = object : TimetableView.TimeTableListener {
override fun onHavaTeacherLongClick(date: Date) {
}
override fun onDefaultClick(date: Date) {
}
override fun onAddClick(date: Date) {
}
override fun onDisableClick(date: Date) {
}
override fun onHavaTeacherSelect(date: Date) {
}
override fun onConFlictSelect(date: Date) {
}
override fun onMissTeacherSelect(date: Date) {
}
}
- 自定义View获取attr+正则表达式
with(typeArr) {
dayNum = getInt(peilian.R.styleable.TimetableView_dayNum, 30)
hourNum = getInt(peilian.R.styleable.TimetableView_hourNum, 10)
hourStep = getInt(peilian.R.styleable.TimetableView_hourStep, 1)
startHour = getInt(peilian.R.styleable.TimetableView_startHour, 9)
startDateStr = getString(peilian.R.styleable.TimetableView_startDate)
?: SimpleDateFormat("yyyy-MM-dd").format(Date())
val pattern = """(\d{4}-\d{2}-\d{2})"""
if (Regex(pattern).matches(startDateStr)) {
startDateStr.split("-").let { strArr ->
startDate = Calendar.getInstance().also { it.set(strArr[0].toInt(), strArr[1].toInt() - 1, strArr[2].toInt()) }.time
}
} else {
throw IllegalStateException("时间参数异常,请检查") as Throwable
}
}
typeArr.recycle()
- 自定义View设置画笔
hStringPaint1 = Paint(Paint.ANTI_ALIAS_FLAG)
hStringPaint1?.let {
it.color = hString1TextColor
it.style = Paint.Style.FILL_AND_STROKE
it.textSize = dp2px(12).toFloat()
it.textAlign = Paint.Align.CENTER
}
- 自定义View读取bitmap
indicateBitmap = readBitMap(mContext, R.drawable.time_table_view_indicate_orange, 30)
fun readBitMap(context: Context, resId: Int, width: Int): Bitmap? {
val options = BitmapFactory.Options()
options.inPreferredConfig = Bitmap.Config.ARGB_4444
options.inJustDecodeBounds = true
//获取资源图片
BitmapFactory.decodeResource(context.resources, resId, options)
options.outHeight = options.outHeight * width / options.outWidth
options.inSampleSize = options.outWidth / width
options.outWidth = width
options.inJustDecodeBounds = false
return BitmapFactory.decodeResource(context.resources, resId, options)
}
- data class和enum
data class Cell(var state: CellState, val date: Date, val minute:Int, var select: Boolean, val point: Point, val rect: Rect, val text: String)
enum class CellState {
DEFAULT,
DISABLE,
MISS_TEACHER,
HAVE_TEACHER,
ADD,
CONFLICT;
}
- dp2px转换扩展
private fun Int.dp2px():Int = QMUIDisplayHelper.dp2px(context, this)
//调用
15.dp2px()
- run+三元表达式
var time = hour.toString() + ":" + minute.toString().run { if (this.length < 2) "0$this" else this }
- for循环+map集合
for (i in 0 until dayNum) {
dayStrList.add(calendar.get(Calendar.DAY_OF_MONTH).toString())
weekMap[calendar.get(Calendar.DAY_OF_WEEK)]?.let { weekStrList.add(it) }
//增加日期中的某个域的值,比如想获得明天的日期,就需要向DAY域加1
calendar.add(Calendar.DAY_OF_MONTH, 1)
}
- map遍历+forEach continue
otherCellMap.forEach continuing@{ it ->
if (it.value.state == CellState.DEFAULT) {
return@continuing
}
it.let {
tmpRectF.left = (it.value.rect.left + cellPoint!!.x).toFloat()
tmpRectF.top = (it.value.rect.top + cellPoint!!.y).toFloat()
tmpRectF.right = (it.value.rect.right + cellPoint!!.x).toFloat()
tmpRectF.bottom = (it.value.rect.bottom + cellPoint!!.y).toFloat()
}
if (isRectInViewPort(tmpRectF.left.toInt(), tmpRectF.top.toInt(), tmpRectF.right.toInt(), tmpRectF.bottom.toInt())) {
val startPointx = it.value.point.x + cellPoint!!.x
val startPointy = it.value.point.y + cellPoint!!.y
stateRectBgPaint?.let { paint ->
when (it.value.state) {
CellState.ADD -> {
paint.color = stateBgColorAdd
canvas?.drawRoundRect(tmpRectF, dp2px(5).toFloat(), dp2px(5).toFloat(), paint)
//画加号
addBitmap?.let { bitmap ->
canvas?.drawBitmap(bitmap, startPointx.toFloat() + (it.value.rect.width() / 2 - bitmap.width / 2), startPointy.toFloat() + (it.value.rect.height() / 2 - bitmap.width / 2), stateBitmapPaint)
}
}
CellState.MISS_TEACHER -> {
paint.color = stateBgColorMissTeacher
canvas?.drawRoundRect(tmpRectF, dp2px(5).toFloat(), dp2px(5).toFloat(), paint)
missTeacherBitmap?.let { bitmap ->
canvas?.drawBitmap(bitmap, startPointx.toFloat(), startPointy.toFloat(), stateBitmapPaint)
}
}
CellState.HAVE_TEACHER -> {
paint.color = stateBgColorHaveTeacher
canvas?.drawRoundRect(tmpRectF, dp2px(5).toFloat(), dp2px(5).toFloat(), paint)
//画老师名字
stateTextPaint!!.color = stateTextColorHaveTeacher
canvas?.let { canvas ->
drawText(canvas, it.value.text, startPointx + it.value.rect.width() / 2, startPointy + it.value.rect.height() / 2, stateTextPaint!!)
}
}
CellState.DISABLE -> {
paint.color = stateBgColorDisable
canvas?.drawRoundRect(tmpRectF, dp2px(5).toFloat(), dp2px(5).toFloat(), paint)
}
CellState.CONFLICT -> {
paint.color = stateBgColorConflict
canvas?.drawRoundRect(tmpRectF, dp2px(5).toFloat(), dp2px(5).toFloat(), paint)
//画老师名字
stateTextPaint!!.color = stateTextColorConflict
canvas?.let { canvas ->
drawText(canvas, it.value.text, startPointx + it.value.rect.width() / 2, startPointy + it.value.rect.height() / 2, stateTextPaint!!)
}
}
else -> paint.color = stateBgColorDisable
}
if (it.value.select) {
currentSelectRect.left = tmpRectF.left
currentSelectRect.right = tmpRectF.right
currentSelectRect.top = tmpRectF.top
currentSelectRect.bottom = tmpRectF.bottom
}
}
// canvas?.drawRoundRect(tmpRectF,dp2px(5).toFloat(),dp2px(5).toFloat(), stateRectBgPaint!!)
} else {//画边界外的indicate
if (it.value.state==CellState.MISS_TEACHER||it.value.state==CellState.HAVE_TEACHER)
drawIndicate(tmpRectF, canvas)
}
}
- 可变参数数组参数传递
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
.let {
requestPermission(*it)
}
//接收
private fun requestPermission(vararg permissions: String) {
val rxPermission = RxPermissions(this)
//请求权限全部结果
val dispose = rxPermission.request(*permissions)
.subscribe { granted ->
if ((!granted!!)) {
ToastUtils.showShortToast("未授权")
finish()
} else {
go2Next()
}
}
compositeDisposable.add(dispose)
}
- onClick事件
ll_start_time_con.setOnClickListener {
showTimePicker(0)
}
先记录这么多,不断更新!
网友评论