第一天寫關於Toast的小分享
有時候想要測試一些點擊時,會用顯示Toast的方式來協助
不過Toast是必須等到前一個Toast的持續時間結束
後一個Toast才會出現
如果我點完第1個button,馬上再點第2個,就須要等前一個Toast顯示完後才會跳第二個
無法跟點擊動作連續顯示二個Toast
有時候想要迅速的查看幾個以上的動作就比較不適合
可設置一個變數專供存取toast使用,搭配cancel()與show()方法就可以比較快速的顯示出來
class MainActivity : AppCompatActivity() {
private lateinit var toast: Toast
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toast = Toast.makeText(this, "", Toast.LENGTH_SHORT)
btn_one.setOnClickListener {
showToast("This is Button 1111")
}
btn_two.setOnClickListener {
showToast("This is Button 2222")
}
}
private fun showToast(string: String) {
toast.cancel()
toast = Toast.makeText(this, "$string", Toast.LENGTH_SHORT)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
}
}
如果想要做點小變化,可以用自定義的layout呈現(圖片,文字等)
例如
<?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:id="@+id/custom_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFEB3B">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:src="@drawable/ic_baseline_done_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.495" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="This is custom toast"
android:textColor="@android:color/holo_orange_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />
<TextView
android:id="@+id/tv_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintBaseline_toBaselineOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
因爲每次點擊時,原本的toast cancel掉了
所以須要再給值toast = Toast(this)
class MainActivity : AppCompatActivity() {
private lateinit var toast: Toast
private lateinit var customLayout: View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
customLayout = layoutInflater.inflate(R.layout.custom_toast,findViewById(R.id.custom_toast))
toast = Toast.makeText(this, "", Toast.LENGTH_SHORT)
btn_one.setOnClickListener {
showToast("1111")
}
btn_two.setOnClickListener {
showToast("2222")
}
}
private fun showToast(string: String) {
toast.cancel()
// toast = Toast.makeText(this, "$string", Toast.LENGTH_SHORT)
toast = Toast(this)
toast.view = customLayout
customLayout.tv_display.text = string
toast.duration = Toast.LENGTH_SHORT
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
}
}