目录1、启动新Activty1.1、功能分析1.2、开发视图布局1.3、按钮事件响应1.4、测试结果2、启动其他App2.1、功能分析2.2、开发视图布局2.3、按钮事件响应2.4、
activity_create_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=".CreateMessageActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint"
android:inputType="textPersonName"
android:textSize="30sp"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onSendMessage"
android:text="@string/send"
android:textSize="30sp"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
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"
app:layout_constraintRight_toRightOf="parent"
tools:context=".ReceiveMessageActivity">
<TextView
android:id="@+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd Activity"
android:textSize="34sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constr
string.xml
<resources>
<string name="app_name">Messager</string>
<string name="send">Send Message</string>
<string name="hint">Enter a message</string>
<string name="choser">Send Message via ...</string>
</resources>
CreateMessageActivty类:发送消息
public class CreateMessageActivity extends AppCompatActivity {
//定义常量,作为消息的key
public static final String MESSAGE_KEY="szst.it.ping.messager";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_message);
}
public void onSendMessage(View Button){
//获得编辑框引用
EditText editText = findViewById(R.id.input);
//取出编辑框文字
String message = editText.getText().toString();
//Intent是Android中的信使,新建Intent打开,设置收件Activity为ReceiveMessageActivity
Intent intent = new Intent(this,ReceiveMessageActivity.class) ;
//在intent中附加消息
intent.putExtra(MESSAGE_KEY,message);
//向Android发出请求
startActivity(intent);
}
}
ReceiveMessageActivty类:接收消息
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);
}
}
启动界面
输入消息“123”并点击按钮发送,接收界面如下
CreateMessageActivty类:发送消息
public class CreateMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_message);
}
public void onSendMessage(View Button){
//获得编辑框引用
EditText editText = findViewById(R.id.input);
//取出编辑框文字
String message = editText.getText().toString();
//使用new Intent(Intent.ACTION_SEND)替换new Intent(this, ReceiveMessageActivity.class),不知道其它App中的类名
Intent intent = new Intent(Intent.ACTION_SEND);
//设置消息类型为纯文本,系统不会对消息进行处理
intent.setType("text/plain");
//向Intent添加附加信息
intent.putExtra(Intent.EXTRA_TEXT,message);
//自定义选择对话框
String chooserTitle = getString(R.string.choser);
Intent chosenIntent = Intent.createChooser(intent, chooserTitle);
startActivity(chosenIntent) ;
}
}
启动界面同1.4
输入消息“123”并点击按钮发送,选择要发送的app(Messaging)
发送附加消息到111
发送成功
到此这篇关于Android实现页面跳的文章就介绍到这了,更多相关Android实现页面跳转内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Android实现页面跳转的全过程记录
本文链接: https://www.lsjlt.com/news/154617.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0