Android Button是用于在单击时执行事件的按钮。它是android.widget.Button类下的一个UI组件。要了解有关Android Button的更多信息, 请参阅Android Button示例
使用Kotlin, 我们可以使用以下方法通过不同的方式在Android Button上执行事件:
1.实现Button的setOnClickListener
button1.setOnClickListener(){
Toast.makeText(this, "button 1 clicked", Toast.LENGTH_SHORT).show()
}
2.实现View.OnClickListner并覆盖其功能
button2.setOnClickListener(this)
. .
override fun onClick(view: View) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
3.在布局文件中添加Button的onClick属性并实现其功能。
<Button
android:onClick="clickButton"/>
fun clickButton(v: View){
val mToast = Toast.makeText(applicationContext, "button 3 clicked", Toast.LENGTH_SHORT)
mToast.show()
}
4.以编程方式创建一个Button并将其设置在布局上
button4.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))
button4.setId(button4_Id)
button4.x = 250f
button4.y = 500f
button4.setOnClickListener(this)
constraintLayout.addView(button4)
Kotlin Android按钮示例
在此示例中, 我们将创建Button并对其执行事件。单击按钮, 显示一条祝酒消息。
activity_main.xml
在activity_main.xml布局文件中的Widgets面板中添加三个Button。其代码如下。 id button3的Button添加了onClick属性, 其函数名称在MainActivity类文件中实现。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_height="match_parent"
android:id="@+id/constraintLayout"
tools:context="example.srcmini02.com.kotlinbutton.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Action Example"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.073"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>
<Button
android:id="@+id/button1"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.762" />
<Button
android:id="@+id/button3"
android:layout_width="101dp"
android:layout_height="48dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="clickButton"
android:text="Button 3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.774" />
</android.support.constraint.ConstraintLayout>
MainActivity.kt
在MainActivity.kt类中添加以下代码。在此类中, 我们在按钮上实现setOnClickListener侦听器, 实现View类(View.OnClickListener)的OnClickListener并覆盖其功能onClick。在该类中, 我们还以编程方式创建一个Button(button4), 定义其属性并将其设置在布局上。
package example.srcmini02.com.kotlinbutton
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() , View.OnClickListener {
val button4_Id: Int = 1111
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button1.setOnClickListener(){
Toast.makeText(this, "button 1 clicked", Toast.LENGTH_SHORT).show()
}
button2.setOnClickListener(this)
// add button dynamically
val button4 = Button(this)
button4.setText("Button 4 added dynamically")
button4.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))
button4.setId(button4_Id)
button4.x = 250f
button4.y = 500f
button4.setOnClickListener(this)
constraintLayout.addView(button4)
}
override fun onClick(view: View) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
when (view.id) {
R.id.button2 ->
Toast.makeText(this, "button 2 clicked", Toast.LENGTH_SHORT).show()//single line code
button4_Id->{//multiline code
val myToast = Toast.makeText(this, "button 4 clicked", Toast.LENGTH_SHORT)
myToast.show()
}
}
}
fun clickButton(v: View){
val mToast = Toast.makeText(applicationContext, "button 3 clicked", Toast.LENGTH_SHORT)
mToast.show()
}
}
输出:
评论前必须登录!
注册