如何在 Kotlin 中创建一个打开新 Activity 的按钮(Android Studio)?

问题描述 投票:0回答:9

您好,我正在使用 Android Studio 和 Kotlin 语言制作应用程序,但在获取打开新活动的按钮时遇到问题。我在 xml 文件中创建了按钮,但找不到如何在 MainActivity.kt 中声明它以及如何创建将我带到新活动的 OnClicklistener 的 KOTLIN 语法。我也在清单中定义了新活动,我想我只需要有关如何实际从 MainActivity.kt 切换到 secondaryActivity.kt 的语法帮助。如有任何帮助,我们将不胜感激。

android android-studio kotlin kotlin-android-extensions kotlin-extension
9个回答
21
投票

您可以添加

onclick
事件监听器,如下所示。

 button1.setOnClickListener(object: View.OnClickListener {
    override fun onClick(view: View): Unit {
        // Handler code here.
        val intent = Intent(context, DestActivity::class.java);
        startActivity(intent);
    }
})

或者您可以使用简化形式

   button1.setOnClickListener {
    // Handler code here.
    val intent = Intent(context, DestActivity::class.java)
    startActivity(intent);
   }

6
投票

布局 xml 文件中的按钮

        <Button
            android:id="@+id/btn_start_new_activity"
            android:text="New Activity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

用于在 Kotlin 活动文件中声明它

var btn_new_activity = findViewById(R.id.btn_start_new_activity) as Button

Onclicklistener 设置为按钮,单击按钮时启动新活动

    btn_new_activity.setOnClickListener {
        val intent = Intent(context, NewActivity::class.java)
        startActivity(intent);
    }

参考:Android Studio教程 - https://www.youtube.com/watch?v=7AcIGyugR7M


2
投票

我建议您使用 Anko - Kotlin 扩展 https://github.com/Kotlin/anko。 它让您以最短的方式使用意图(以及更多其他东西)。在你的情况下,它将是:

button {
        onClick { startActivity<SecondActivity>() }
    }

2
投票
// In your method `fun onCreate(savedInstanceState: Bundle?)` add this.

    your_btn_id.setOnClickListener{

                val intent = Intent(this, yourpagename::class.java)
                startActivity(intent)
            }

// till now if it doesn't work then, check if these two files are added or not,

    import android.content.Intent
    import kotlinx.android.synthetic.main.activity_otp.*

//希望它能起作用。 😊


2
投票

科特林

确保该项目位于

OnCreate
方法内。 编辑
XML
文件(res/layout 文件夹)并创建按钮:

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Press me"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginStart="40dp"
    android:layout_marginEnd="40dp"
    android:layout_marginBottom="40dp"
    />

接下来是主要活动课:

class MainActivity : AppCompatActivity() {

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

        val button: Button = findViewById(R.id.button)
        button.setOnClickListener {
           
           val intent = Intent(context, NewActivity::class.java)
           startActivity(intent);
        }
    }
}

1
投票

您可以创建一个通用方法来启动任何 Activity

  inline fun<reified T> launchActivity(){
    val intent = Intent(this, T::class.java)
    startActivity(intent)
}

并且可以像这样使用

 button1.setOnClickListener {
      launchActivity<AnyActivity>()
}

要获取有关具体化的更多详细信息转到此处


0
投票

您可以简单地在主活动中声明您的按钮,如下所示:

val button = findViewById<Button>(R.id.button)
        button.setOnClickListener(this);

并在点击侦听器中启动新活动:

override fun onClick(p0: View?) {
        val intent = Intent(this, activity::class.java)
        startActivity(intent)
    }

0
投票

我必须先在 build.gradle 的插件中添加

id 'kotlin-android-extensions'
。 之后在 OnCreate 中 Button.setOnClickListener { }


0
投票

我尝试了所有的方法,但都不起作用,但我不认为我做错了,因为我的兄弟是一名专业编码员,他说我做得正确。

© www.soinside.com 2019 - 2024. All rights reserved.