Android studio基础练习04【两页面之间的跳转】

一、结构搭建

Android studio基础练习04【两页面之间的跳转】_第1张图片

二、代码实现

  AndroidManifest.xml




    
        
        
            
                
                

                
                

                
                
            
        
    

  activity_create_message.xml




    

        

        

  activity_receive_message.xml 




    

  strings.xml


    My Messenger
    Send Message
    Enter a message
    Send message via...

  CreateMessageActivity

package com.suke.messager;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class CreateMessageActivity extends AppCompatActivity {
    //定义常量,作为消息的KEY
    public static final String MESSAGE_KEY = "com.suke.message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_message);
    }

    public void onSendMessage(View view) {
        //获得编辑框引用
        EditText editText = findViewById(R.id.input);
        //取出编辑框文字
        String message = editText.getText().toString();

        Intent intent1 = new Intent(this, ReceiveMessageActivity.class);
        //再intent中附加消息
        intent1.putExtra(MESSAGE_KEY, message);

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, message);
        String chooserTitle = getString(R.string.chooser);
        Intent chosenIntent = Intent.createChooser(intent, chooserTitle);
        intent.putExtra(MESSAGE_KEY, message);
        startActivity(intent1);
        startActivity(chosenIntent);
    }
}

  ReceiveMessageActivity

package com.suke.messager;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ReceiveMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_message);
        //获得intent引用
        Intent intent = getIntent();
        //根据KEY取出value
        String message = intent.getStringExtra(CreateMessageActivity.MESSAGE_KEY);
        //获得文本框引用,设置文字
        TextView textView = findViewById(R.id.output);
        textView.setText(message);
    }
}

三、执行效果

Android studio基础练习04【两页面之间的跳转】_第2张图片Android studio基础练习04【两页面之间的跳转】_第3张图片Android studio基础练习04【两页面之间的跳转】_第4张图片

你可能感兴趣的:(Android,Studio,android,studio,java)