【问题标题】:Image button to open new Activity打开新活动的图像按钮
【发布时间】:2022-03-02 15:12:01
【问题描述】:

我对此很陌生,我唯一的编程经验是使用 PLC;s 和 HMI,所以我很抱歉无法弄清楚什么可能是一件容易的事。但我找不到任何当前视频或工作示例,说明如何简单地将图像放在主要活动上并在按下后打开另一个活动。我正在使用最新版本的 Android Studio 11.0 并尝试学习 Kotlin 语言。我已经附上了我目前拥有的项目的代码,正如你所看到的,我刚刚开始了这个项目。提前感谢您的帮助。

Java 文件

package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
 override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     setContentView(R.layout.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:layout_height="match_parent"
    tools:context=".MainActivity">```

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="327dp"
        android:layout_height="477dp"
        android:layout_marginStart="38dp"
        android:layout_marginTop="170dp"
        android:layout_marginEnd="38dp"
        android:layout_marginBottom="84dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/trafficlightoriginal" />

</androidx.constraintlayout.widget.ConstraintLayout>
 
 
       
 
        

【问题讨论】:

  • 学习onClickListeners
  • 这可能会有所帮助link

标签: android android-studio button android-activity


【解决方案1】:

为图像按钮设置一个 onClickListener,然后使用一个意图导航到下一个活动

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

val imageButton:ImageButton=findViewById(R.id.imageButton)
imageButton.setOnClickListener{
startActivity(Intent(this,SecondActivity::class.java))
}
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多