Android intent的例子

activity_create_message.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CreateMessageActivity">

    <EditText
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"/>

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="전송"
        android:layout_below="@+id/message"/>


</RelativeLayout>

activity_receive_message.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ReceiveMessageActivity">


    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="message"/>

</androidx.constraintlayout.widget.ConstraintLayout>

strings.xml

<resources>
    <string name="app_name">Messenger</string>
    <string name="chooser">Ssend message</string>
</resources>

CreateMessageActivity

package com.example.messenger;

import androidx.appcompat.app.AppCompatActivity;

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

public class CreateMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_message);
        Button send=(Button)findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
//  명시적인 인텐트 (explicit intent)
//               EditText message=(EditText)findViewById(R.id.message);
//               String messageText=message.getText().toString();
//               Intent intent=new Intent(CreateMessageActivity.this,ReceiveMessageActivity.class);
//               intent.putExtra(ReceiveMessageActivity.EXTRA_MESSAGE,messageText);
//               startActivity(intent);

//  암시적인 인텐트 (implicit intent) 不需要ReceiveMessageActivity.java
                EditText message=(EditText)findViewById(R.id.message);
                String messageText=message.getText().toString();
                Intent intent =new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
               intent.putExtra(Intent.EXTRA_TEXT,messageText);
               startActivity(intent);
//메세지 애플 어느거 할지 선택할수 있다
//               String chooserTitle =getString(R.string.chooser);
//               Intent chosenIntent=Intent.createChooser(intent,chooserTitle);
//               startActivity(chosenIntent);
           }
       });


    }

}


ReceiveMessageActivity

package com.example.messenger;

import androidx.appcompat.app.AppCompatActivity;

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

public class ReceiveMessageActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE="message";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_message);

        Intent intent=getIntent();
        String messageText=intent.getStringExtra(EXTRA_MESSAGE);
        TextView message=(TextView)findViewById(R.id.message);
        message.setText(messageText);


    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值