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


当前回答

Intent i = new Intent(firstactivity.this, secondactivity.class);
startActivity(i);

其他回答

为ViewPerson活动创建一个意图并传递PersonID(例如,用于数据库查找)。

Intent i = new Intent(getBaseContext(), ViewPerson.class);                      
i.putExtra("PersonID", personID);
startActivity(i);

然后在ViewPerson Activity中,你可以获得额外的数据包,确保它不为空(以防你有时不传递数据),然后获取数据。

Bundle extras = getIntent().getExtras();
if(extras !=null)
{
     personID = extras.getString("PersonID");
}

现在如果你需要在两个activity之间共享数据,你也可以有一个全局单例。

public class YourApplication extends Application 
{     
     public SomeDataClass data = new SomeDataClass();
}

然后在任何活动中调用它:

YourApplication appState = ((YourApplication)this.getApplication());
appState.data.CallSomeFunctionHere(); // Do whatever you need to with data here.  Could be setter/getter or some other type of logic

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

   //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"

//在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")
Intent iinent= new Intent(Homeactivity.this,secondactivity.class);
startActivity(iinent);

一件容易的事。

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
CurrentActivity.this.startActivity(myIntent);

额外的内容可以通过以下方式从另一侧检索:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    String value = intent.getStringExtra("key"); //if it's a string you stored.
}

不要忘记在AndroidManifest.xml中添加你的新活动:

<activity android:label="@string/app_name" android:name="NextActivity"/>