欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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>

Android studio :活动间的通信
Android studio :活动间的通信
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>

Android studio :活动间的通信
Android studio :活动间的通信
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>

Android studio :活动间的通信
Android studio :活动间的通信

本文地址:https://blog.csdn.net/ITarmi/article/details/107207572