Android studio :活动间的通信
程序员文章站
2022-04-19 14:01:48
PutActivityMainActivity.javapackage com.example.putactivity;import android.content.Intent;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity ext...
PutActivity
MainActivity.java
package com.example.putactivity;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Bundle bundle = new Bundle();//创建bundle对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle.putString("key1","小明");//bandle对象携带压入字符串数据
bundle.putInt("key2",18);//bandle对象压入整形数据
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtras(bundle);//使用Intetnt携带bundle对象
startActivity(intent);//启动活动
}
});
}
}
Main2Activity.java
package com.example.putactivity;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
Intent intent;//定义Intent对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView tv1 = findViewById(R.id.tv1);
TextView tv2 = findViewById(R.id.tv2);
intent = getIntent();//获取intent对象
//通过intent对象获取数据
String name = intent.getStringExtra("key1");
int age = intent.getIntExtra("key2",0);
tv1.setText("用户名为:"+name);//显示数据
tv2.setText("年龄为:"+age);
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动新活动"/>
</LinearLayout>
Activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Main2Activity"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
ResultActivity
MainActivity.java
package com.example.resultactivity;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
//第一个参数是intent,第二个参数是请求码,随便定义,保证是唯一的就行
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//这里要通过请求码来判断数据的来源
switch (requestCode) {
case 1:
//判断请求的结果是否成功,resultCode == RESULT_OK,代表成功了
if (resultCode == RESULT_OK) {
String name = data.getStringExtra("key1");
String age = data.getStringExtra("key2");
Toast.makeText(MainActivity.this, "姓名:"+name+"年龄:"+age, Toast.LENGTH_LONG).show();
}
break;
/**这里可以有多个requestcode**/
default:
}
}
}
Main2Activity.java
package com.example.resultactivity;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main2Activity extends AppCompatActivity {
String name;//定义名称字符串串
String age;//定义年龄整形
EditText et1,et2;//定义编辑框对象
Intent intent;//定义Intent对象
Bundle bundle = new Bundle();//定义bundle对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
et1 = findViewById(R.id.et1);
et2 = findViewById(R.id.et2);
Button btn = findViewById(R.id.btn);
//设置按钮监听事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent();//定义Intent
//获取用户输入数据
name = et1.getText().toString();
age = et2.getText().toString();
//压入数据
bundle.putString("key1",name);
bundle.putString("key2",age);
intent.putExtras(bundle);//传递bundle对象
setResult(RESULT_OK,intent);//设置返回消息码
finish();//关闭该活动页面
}
});
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:orientation="vertical">
<EditText
android:id="@+id/et1"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"/>
<EditText
android:id="@+id/et2"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄"/>
<Button
android:id="@+id/btn"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
Activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转至新页面"/>
</LinearLayout>
StaticActivity
MainActivity.java
package com.example.staticactivity;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//给新活动中的静态成员赋值
Main2Activity.name = "小明";
Main2Activity.age = 18;
//创建Intent对象
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);//启动活动
}
});
}
}
Main2Activity.java
package com.example.staticactivity;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
public static String name="";
public static int age=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//将传入的数据打印显示
Toast.makeText(Main2Activity.this,"姓名:"+name+" 年龄:"+age,Toast.LENGTH_SHORT).show();
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转至新页面"/>
</LinearLayout>
Activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:orientation="vertical">
<TextView
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新活动"/>
</LinearLayout>
本文地址:https://blog.csdn.net/ITarmi/article/details/107207572
上一篇: mysql 优化方案,纯干活
下一篇: 浅谈mysql的查询过程
推荐阅读
-
【Android开发】Android Studio 打开AVD遇到的问题
-
Android学习笔记(Android Studio) 4-2-1~2 Fragment详解(一、二)(不可不会的Activity和Fragment)
-
Android学习笔记(Android Studio) 4-1-2 Activity的生命周期(不可不会的Activity和Fragment)
-
android studio 3.0.1依赖butterknife报错问题解决办法以及androidstudio2.0和3.0以上butterknife的配置大全
-
读《 NFC:Arduino、Android与PhoneGap近场通信》 有感——智能的电磁场交互
-
解决SpannableString在Android组件间传递时显示失效的问题
-
Android的线程通信:消息机制原理(Message,Handler,MessageQueue,Looper),异步任务AsyncTask,使用JSON
-
PHP下操作Linux消息队列完成进程间通信的方法
-
Mac OS下为Android Studio编译FFmpeg解码库的详细教程
-
Android React Native原生模块与JS模块通信的方法总结