在Android应用程序中,当单击另一个活动中的按钮时,如何启动一个新活动(GUI),以及如何在这两个活动之间传递数据?


当前回答

//在Kotlin中,你可以这样做 /*在第一个活动中,让在活动布局中有一个id为按钮的按钮。 假设我必须将数据作为String类型从一个活动传递到另一个*/

     val btn = findViewById<Button>(R.id.button)
     btn.setOnClickListener {
        val intent = Intent(baseContext, SecondActivity::class.java).apply {
             putExtra("KEY", data)
        }
        startActivity(intent)
     }

//在第二个活动中,你可以从另一个活动中获取数据

 val name = intent.getStringExtra("KEY")

/*假设你必须传递一个自定义对象,那么它应该是Parcelable。 让有类拼贴类型,我必须从一个活动传递到另一个 * /

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
class Collage(val name: String, val mobile: String, val email: String) : Parcelable

首先,让这里的数据是拼贴类型。我必须把它交给另一个活动。* /

val btn = findViewById<Button>(R.id.button)
         btn.setOnClickListener {
            val intent = Intent(baseContext, SecondActivity::class.java).apply {
                 putExtra("KEY", data)
            }
            startActivity(intent)
         }

//那么从第二个Activity中我们将得到as

val item = intent.extras?.getParcelable<Collage>("KEY")

其他回答

在发送活动中尝试以下代码

   //EXTRA_MESSAGE is our key and it's value is 'packagename.MESSAGE'
    public static final String EXTRA_MESSAGE = "packageName.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ....

        //Here we declare our send button
        Button sendButton = (Button) findViewById(R.id.send_button);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //declare our intent object which takes two parameters, the context and the new activity name

                // the name of the receiving activity is declared in the Intent Constructor
                Intent intent = new Intent(getApplicationContext(), NameOfReceivingActivity.class);

                String sendMessage = "hello world"
                //put the text inside the intent and send it to another Activity
                intent.putExtra(EXTRA_MESSAGE, sendMessage);
                //start the activity
                startActivity(intent);

            }

从接收活动尝试以下代码:

   protected void onCreate(Bundle savedInstanceState) {
 //use the getIntent()method to receive the data from another activity
 Intent intent = getIntent();

//extract the string, with the getStringExtra method
String message = intent.getStringExtra(NewActivityName.EXTRA_MESSAGE);

然后将以下代码添加到AndroidManifest.xml文件中

  android:name="packagename.NameOfTheReceivingActivity"
  android:label="Title of the Activity"
  android:parentActivityName="packagename.NameOfSendingActivity"
    Intent in = new Intent(getApplicationContext(),SecondaryScreen.class);    
    startActivity(in);

    This is an explicit intent to start secondscreen activity.

一个老问题,但如果目标是切换显示的页面,我只有一个活动,当我想切换页面时调用setContentView()(通常是响应用户单击按钮)。这允许我简单地从一个页面的内容调用到另一个页面。没有意图疯狂的额外包裹捆绑和任何试图来回传递数据。

我像往常一样在分辨率/布局中做了一堆页面,但没有为每个页面做一个活动。只需使用setContentView()来根据需要切换它们。

所以我的一个和唯一的onCreate()有:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater layoutInflater = getLayoutInflater();

    final View mainPage = layoutInflater.inflate(R.layout.activity_main, null);
    setContentView (mainPage);
    Button openMenuButton = findViewById(R.id.openMenuButton);

    final View menuPage = layoutInflatter.inflate(R.layout.menu_page, null);
    Button someMenuButton = menuPage.findViewById(R.id.someMenuButton);

    openMenuButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(menuPage);
        }
    });

    someMenuButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            do-something-interesting;
            setContentView(mainPage);
        }
    }
}

如果你想要返回按钮在退出应用程序之前返回内部页面,只需包装setContentView()将页面保存在一个小的页面堆栈中,并在onBackPressed()处理程序中弹出这些页面。

点击按钮打开活动的最简单的方法是:

在res文件夹下创建两个活动,在第一个活动中添加一个按钮,并为onclick函数命名。 每个活动应该有两个java文件。 下面是代码:

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void goToAnotherActivity(View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}

SecondActivity.java

package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);
    }
}

AndroidManifest.xml(只需将此代码块添加到现有代码中)

 </activity>
        <activity android:name=".SecondActivity">
  </activity>
 imageView.setOnClickListener(v -> {
// your code here
        });