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


当前回答

在第一个活动中编写代码。

button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


Intent intent = new Intent(MainActivity.this, SecondAcitvity.class);
                       //You can use String ,arraylist ,integer ,float and all data type.
                       intent.putExtra("Key","value");
                       startActivity(intent);
                        finish();
            }
         });

在secondActivity.class

String name = getIntent().getStringExtra("Key");

其他回答

虽然已经提供了正确的答案,但我在这里搜索语言Kotlin的答案。这个问题不是关于特定于语言的,所以我添加代码来完成这个任务在Kotlin语言。

下面是如何在Kotlin中为andorid做到这一点

testActivityBtn1.setOnClickListener{
      val intent = Intent(applicationContext,MainActivity::class.java)
      startActivity(intent)

 }

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

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

当用户点击按钮时,直接在XML中,就像这样:

<Button
         android:id="@+id/button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="TextButton"
         android:onClick="buttonClickFunction"/>

使用属性android:onClick我们声明方法名必须出现在父活动上。所以我必须在我们的活动中创建这个方法,像这样:

public void buttonClickFunction(View v)
{
            Intent intent = new Intent(getApplicationContext(), Your_Next_Activity.class);
            startActivity(intent);
}

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

一个老问题,但如果目标是切换显示的页面,我只有一个活动,当我想切换页面时调用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()处理程序中弹出这些页面。