gpt4 book ai didi

android - 如何在Kotlin Android Studio中制作可长时间单击的清除按钮

转载 作者:行者123 更新时间:2023-12-02 12:49:43 25 4
gpt4 key购买 nike

我有一个写在Kotlin上的计算器,上面有一个清晰的按钮,
我想使这个按钮清晰明了:如果您按下它,它将开始从字符串中删除文本,
并且每秒删除的速度越来越快,就像在Google计算器中一样
清除按钮Kotlin代码:

buttonClear.setOnClickListener {
string.text = string.text.toString().droplast(1)
}
Android Studio前端按钮代码:
<Button
android:id="@+id/buttonClear"
android:longClickable="true"
android:text="C"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="1.0" />
github的完整代码

最佳答案

我做了一个示例项目。您可以更改代码中的参数,以便此功能可以更快/更慢地删除字符等。activity_main.xml:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity"
>

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:text="@string/longStringTest"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt:
import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


private const val charsDeletePerTimeStart = 1 // number of chars that will be deleted at the beginning
private const val charsDeletePerTimeIncrement = 1 // number of chars that will increase number of deleting per `timePeriod`
private const val timePeriod = 100L //time period in milliseconds, every 100ms chars will be deleted

class MainActivity : AppCompatActivity()
{
private val handler: Handler = Handler()
private var charsToDelete = charsDeletePerTimeStart //actual number of deleting chars


@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

fun deleteChars() // this function delete last characters from EditText and increment `timePeriod`
{
editText.setText(editText.text.toString().dropLast(charsToDelete))
Log.d("MyTag", "Deleted chars $charsToDelete")
charsToDelete += charsDeletePerTimeIncrement
}

button.setOnTouchListener { _, e -> // setOnTouchListener, this will allow to start deleting chars
if (e != null)
{
when (e.action)
{
MotionEvent.ACTION_DOWN -> //click -> start deleting
{
charsToDelete = charsDeletePerTimeStart
deleteChars()
handler.postDelayed(object : Runnable
{
override fun run()
{
deleteChars()
handler.postDelayed(this, timePeriod)
}
}, timePeriod)
}
MotionEvent.ACTION_UP -> // stop -> clear handler
{
handler.removeCallbacksAndMessages(null)
}
}
}
return@setOnTouchListener true
}
}
}
结果(logcat)。每〜100ms个字符将被删除,而每次又增加一个字符。
00:46:59.080  -  Deleted chars 1
00:46:59.189 - Deleted chars 2
00:46:59.297 - Deleted chars 3
00:46:59.406 - Deleted chars 4
00:46:59.513 - Deleted chars 5 // Here I stop deleting
00:47:02.957 - Deleted chars 1 // start again
00:47:03.063 - Deleted chars 2
00:47:03.170 - Deleted chars 3
00:47:03.276 - Deleted chars 4
00:47:03.381 - Deleted chars 5
00:47:03.486 - Deleted chars 6

关于android - 如何在Kotlin Android Studio中制作可长时间单击的清除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63801950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com