4.22

所花时间(包括上课):1

打码量(行):200

博客量(篇):1

了解到知识点:学习Intent

 

 package com.example.myapp;

 

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

 

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    private static final String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        Button button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                sendMessage();

            }

        });

    }

 

    public void sendMessage() {

        Intent intent = new Intent(this, DisplayMessageActivity.class);

        String message = "Hello, Second Activity!";

        intent.putExtra(EXTRA_MESSAGE, message);

        startActivity(intent);

    }

}

package com.example.myapp;

 

import android.content.Intent;

import android.os.Bundle;

import android.widget.TextView;

 

import androidx.appcompat.app.AppCompatActivity;

 

public class DisplayMessageActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_display_message);

 

        Intent intent = getIntent();

        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

 

        TextView textView = findViewById(R.id.textView);

        textView.setText(message);

    }

}

posted @ 2024-04-22 20:41  赵千万  阅读(2)  评论(0编辑  收藏  举报