首页 > 其他分享 >2022-10-17学习内容

2022-10-17学习内容

时间:2022-10-17 23:13:38浏览次数:40  
标签:10 17 tv request bundle Bundle 2022 import response

1.向下一个Activity发送数据

1.1activity_act_send.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="今天天气真不错"/>

    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="发送以上文字"/>

</LinearLayout>

1.2ActSendActivty.java

package com.example.chapter04;

import androidx.appcompat.app.AppCompatActivity;

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

import com.example.chapter04.utils.DateUtil;

public class ActSendActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_send);
        tv_send = findViewById(R.id.tv_send);
        findViewById(R.id.btn_send).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, ActReceiveActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("request_time", DateUtil.getNowTime());
        bundle.putString("request_content", tv_send.getText().toString());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

1.3ActReceiveActivity.java

package com.example.chapter04;

import androidx.appcompat.app.AppCompatActivity;

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

public class ActReceiveActivity extends AppCompatActivity {

    private TextView tv_receive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_receive);
        tv_receive = findViewById(R.id.tv_receive);
        // 从上一个页面传来的意图中获取快递包裹
        Bundle bundle = getIntent().getExtras();
        String request_time = bundle.getString("request_time");
        String request_content = bundle.getString("request_content");
        String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s", request_time, request_content);
        tv_receive.setText(desc);
    }
}

1.4效果:

2.向上一个Activity返回数据

2.1activity_act_request.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="传送请求数据"/>

    <TextView
        android:id="@+id/tv_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

2.2ActRequestActivity.java

package com.example.chapter04;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.chapter04.utils.DateUtil;

public class ActRequestActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String mRequest = "你吃了吗?吃饭去吧";
    private ActivityResultLauncher<Intent> register;
    private TextView tv_response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_request);
        TextView tv_request = findViewById(R.id.tv_request);
        tv_request.setText("待发送的消息为:" + mRequest);

        tv_response = findViewById(R.id.tv_response);

        findViewById(R.id.btn_request).setOnClickListener(this);

        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result != null) {
                Intent intent = result.getData();
                if (intent != null && result.getResultCode() == Activity.RESULT_OK) {
                    Bundle bundle = intent.getExtras();
                    String response_time = bundle.getString("response_time");
                    String response_content = bundle.getString("response_content");
                    String desc = String.format("收到返回消息:\n应答时间为%s\n应答内容为%s", response_time, response_content);
                    // 把返回信息的详情显示在文本视图上
                    tv_response.setText(desc);
                }
            }

        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, ActResponseActivity.class);
        // 创建一个包裹
        Bundle bundle = new Bundle();
        bundle.putString("request_time", DateUtil.getNowTime());
        bundle.putString("request_content", mRequest);
        intent.putExtras(bundle);
        register.launch(intent);
    }
}

2.3activity_act_response.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="返回应答数据"/>

    <TextView
        android:id="@+id/tv_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

2.4ActResponseActivity.java

package com.example.chapter04;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.chapter04.utils.DateUtil;

public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String mResponse = "我还没吃饭,那我们一起吃饭去吧。";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_response);
        TextView tv_request = findViewById(R.id.tv_request);
        // 从上一个页面传来的意图中获取快递包裹
        Bundle bundle = getIntent().getExtras();
        String request_time = bundle.getString("request_time");
        String request_content = bundle.getString("request_content");
        String desc = String.format("收到请求消息:\n请求时间为%s\n请求内容为%s", request_time, request_content);
        // 把请求信息的详情显示在文本视图上
        tv_request.setText(desc);

        findViewById(R.id.btn_response).setOnClickListener(this);

        TextView tv_response = findViewById(R.id.tv_response);
        tv_response.setText("待返回的消息为:" + mResponse);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("response_time", DateUtil.getNowTime());
        bundle.putString("response_content", mResponse);
        intent.putExtras(bundle);
        // 携带意图返回上一个页面,RESULT_OK表示处理成功
        setResult(Activity.RESULT_OK, intent);
        // 结束当前的活动页面
        finish();
    }
}

2.5效果:

 

 

 

 

 

标签:10,17,tv,request,bundle,Bundle,2022,import,response
From: https://www.cnblogs.com/pingfanliliang/p/16797813.html

相关文章

  • 10.17
    #include<stdio.h>intmain(){ unsignedlonglongn,i,m=1; scanf("%llu",&n); for(i=1;;i++) {if(n/10==0) {break; }else{ n=n/10; m++;} } printf("%llu",m......
  • 【LeetCode】1732. 找到最高海拔(C++)
    1732.找到最高海拔(C++)​​1题目描述​​​​2示例描述​​​​2.1示例1​​​​2.2示例2​​​​3解题提示​​​​4源码详解(C++)​​1题目描述有一个自行车手打算......
  • 2022下半年 Acwing 第一篇:快排模板
    模板内容:C++voidquick(intq[],intl,intr){if(l>=r)return;intx=q[(l+r+1)>>1],i=l-1,j=r+1;while(i<j){doi++;while(q[i]<x);......
  • 2022NOIPA层联测10 10月17日
    一句话总结:T1不会,T2多\(\log\)而且写挂了,T3T4没看,56分离场。部分题解T1.异或(xor)推了一大堆没用的结论,没想到分治。题解:从高位到低位处理,对于每一层,如果当前这段......
  • 1033 旧键盘打字(JAVA)
    旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样?输入格式:输入在2行中分别给出坏......
  • P4310 绝世好题
    题意:给出n个数,求最长子序列(不是子数组)的长度,使得其与运算的结果不为0。解:位运算的好处是和顺序无关。第一想法是找每一位为1的最多有几个数。但考虑3,7这种二进制下全是1的......
  • CS5525支持10-100V宽电压输入,2.5A峰值输出电流,DC降压转换器
    ​​​​CS5525E是一种高效的,与高端mosfet集成的异步降压转换器。它提供了2.5A的输出,输入电压可高达100V。CS5525E采用恒定时间(COT)控制模式,提供快速的瞬态响应,便于回路稳定......
  • 【LeetCode】面试题 17.04. 消失的数字(C++)
    面试题17.04.消失的数字(C++)​​1题目描述​​​​2示例描述​​​​2.1示例1​​​​2.2示例2​​​​3解题思路​​​​4源码详解(C++)​​1题目描述数组nums包含从......
  • 【LeetCode】1758. 生成交替二进制字符串的最少操作数(C++)
    1758.生成交替二进制字符串的最少操作数(C++)​​1题目描述​​​​2示例描述​​​​2.1示例1​​​​2.2示例2​​​​2.3示例3​​​​3解题提示​​​​4解题思路......
  • 【LeetCode】1773. 统计匹配检索规则的物品数量(C++)
    1773.统计匹配检索规则的物品数量(C++)​​1题目描述​​​​2示例描述​​​​2.1示例1​​​​2.2示例2​​​​3解题提示​​​​4解题思路​​​​5源码详解(C++)​......