如何使 Android XML 或 Kotlin 按钮将我带到我的其他 XML 活动中?

huangapple go评论106阅读模式
英文:

How to make android XML or Kotlin button take me to my other activity in XML?

问题

我没有一个 Java 文件,原因不明。我有一个 Kotlin 中的 "Main Activity"。因此,我想知道是否能够添加一个按钮,当点击它时,可以跳转到我的另一个活动。

这是主活动的代码:

package com.example.wedapp

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

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

这是我创建的另一个活动,应该打开 pg 活动:

package com.example.wedapp

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

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

我有以下文件:

  • layout\main.xml
  • layout\pg1.xml

MainActivity 是一个 Kotlin 类,pg1 也是一个 Kotlin 文件。

英文:

I don't have a java file for some reason. i have a "Main Activity" in Kotlin. Hence i was wondering if i would be able to add a button which on click takes me to my other activity?

heres the main activity code :


import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat.startActivity


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

here's the other activity i made which should open the pg activity :


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

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

the files i have :
layout\main.xml and layout\pg1.xml

MainActivity is a kotlin clss and pg1 is a kotlin file

答案1

得分: 0

你需要创建第二个活动,它将在按钮点击时打开。

以下是如何在编程中实现的示例代码

button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      openNewActivity();
   }
});

public void openNewActivity(){
  Intent intent = new Intent(this, NewActivity.class);
  startActivity(intent);
}
英文:

You need to create second activity which will be opened on button click

Here's how you can do in coding

    button = (Button) findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            openNewActivity();
         }
      });
   
   public void openNewActivity(){
      Intent intent = new Intent(this, NewActivity.class);
      startActivity(intent);
   }

答案2

得分: 0

将一个按钮添加到你的main.xml文件中,然后给它一个像"switch_scene"的ID。
然后进入你的主活动(main activity)并输入以下代码:
假设你已启用Kotlin扩展,它允许你直接从XML引用ID。

switch_scene.setOnClickListener {
    val newIntent = Intent(this, pg1::class.java)
    startActivity(newIntent)
}

请原谅我如果我犯了错误,你知道我在手机上编码,因为我不在家。

英文:

Add a button first to your main.xml, then give it an id like "switch_scene"..
Then go to your main activity and type the code
Am assuming you have kotlin extension enabled.. that allows you to reference IDs from xml directly..
.

  switch_scene.addOnClickListener{
     val newIntent= Intent(this, pg1::class.java)
     startActivity (newIntent)
 } 

Forgive me if I made mistakes.. am coding with my phone you see as I am not at home.

huangapple
  • 本文由 发表于 2020年8月3日 13:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63223997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定