Android intent的例子
程序员文章站
2022-07-13 17:54:17
...
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);
}
}
上一篇: 静态static关键字一些用法
下一篇: 单向链表反转(含图解)