We are trying to create a Snackbar. The code from a Java app was converted using the Java to Kotlin converter in Android Studio. Next we looked at various examples on many different websites and even tried to implement the code from the book Kotlin Programming Cookbook. We will post all our non working examples below. Our question is how to create the proper syntax to show a Snackbar? We would like to click a btnSNACK with a onClick=onSNACK to show the Snackbar
这是我们的Java-to-Kotlin转换器代码,我们非常想使用这一个
fun onSNACK(view: View){
//Snackbar(view)
//val snackbar = Snackbar(view, "Permission Granted", Snackbar.LENGTH_LONG).setAction("Action", null).show()
snackbar.make(view, "Replace with your own action",
snackbar.LENGTH_LONG).setAction("Action", null).show()
snackbar.setActionTextColor(Color.BLUE)
val snackbarView = snackbar.getView()
snackbarView.setBackgroundColor(Color.LTGRAY)
val textView =
snackbarView.findViewById(android.support.design.R.id.snackbar_text)
textView.setTextColor(Color.BLUE)
textView.setTextSize(28f)
snackbar.show()
}
下一次try 是使用这个代码
class Snackbar{
object LENGTH_LONG {
}
fun show() {
}
}
fun onSNACK(view: View){
snackbar = Snackbar.make(this, "Welcome to Android Teachers..!!",
Snackbar.LENGTH_LONG)
snackbar.show()
}
我们的布局是有Snackbar的活动的相对布局(RL)
class Snackbar(view: View?): Any() {
object LENGTH_SHORT {}
fun View.snack(message: String, length: Int = Toast.LENGTH_LONG, f: Snackbar.
() -> Unit) {
val snack = Snackbar.make(this.findViewById(R.id.RL), message, length)
snack.f()
snack.show()
}
We thought this would work the first line of code was declared top level
lateinit var snackbar: Snackbar//top level
fun onSNACK(){
btnSNACK.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}
我们使用了Snackbar类,有和没有这些不同的方法.