Skip to content

Commit

Permalink
add a widget
Browse files Browse the repository at this point in the history
  • Loading branch information
REBOOTERS committed Apr 15, 2024
1 parent e7bf932 commit 2a2cc85
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.engineer.imitate.ui.activity

import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import com.engineer.imitate.databinding.ActivityGroundDuBinding

class GroundDuActivity : AppCompatActivity() {
private lateinit var viewBinding: ActivityGroundDuBinding

private var show = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewBinding = ActivityGroundDuBinding.inflate(layoutInflater)
setContentView(viewBinding.root)

viewBinding.testButton.setOnClickListener {
if (show) {
viewBinding.loadingText.hide()
} else {
viewBinding.loadingText.show()
}
show = !show
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.engineer.imitate.ui.widget.custom

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView

/**
* 用于显示 。。。 的 loading 态 TextView
*/
class LoadingTextView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, style: Int = 0
) : AppCompatTextView(context, attrs, style) {


private var dotCount = 0
private val handler = Handler(Looper.getMainLooper())

init {
text = ""
}

private val loadingTask = object : Runnable {
override fun run() {
if (dotCount < 4) {
this@LoadingTextView.append(".")
dotCount++
} else {
dotCount = 1
this@LoadingTextView.text = "."// 重置为单个点
}
handler.postDelayed(this, 500); // 每 500 毫秒更新一次
}

}

fun show() {
hide()
handler.post(loadingTask)
}

fun hide() {
text = ""
handler.removeCallbacks(loadingTask)
text = ""
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
hide()
}

}
1 change: 1 addition & 0 deletions imitate/src/main/module/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<activity
android:name=".ui.activity.ninepoint.NinePointActivity"
android:exported="true" />
<activity android:name=".ui.activity.GroundDuActivity" android:exported="true" />

<receiver
android:name=".receivers.AlarmReceiver"
Expand Down
29 changes: 29 additions & 0 deletions imitate/src/main/res/layout/activity_ground_du.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.engineer.imitate.ui.widget.custom.LoadingTextView
android:textColor="@color/black"
android:id="@+id/loading_text"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/test_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 2a2cc85

Please sign in to comment.