android应用案例之Activity上一页面与下一页面返回参数
程序员文章站
2022-03-13 17:08:17
...
一、效果展现
二、代码实现
UI设计部分
activity_main.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"
android:orientation="vertical"
tools:context=".IntentActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_transafer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="传送请求参数"/>
</LinearLayout>
</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"
android:orientation="vertical"
tools:context=".Intent2Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_transafer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回应答参数"/>
</LinearLayout>
</LinearLayout>
功能实现部分
MainActivity.java
package com.example.test_210715;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class IntentActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_response;//声明一个编辑框对象
private TextView tv_response;//声明一个文本试图对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent);
findViewById(R.id.btn_transafer).setOnClickListener(this);
et_response = findViewById(R.id.et_response);
//从布局文件中获取名叫tv_response的文本视图
tv_response = findViewById(R.id.tv_response);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_transafer){
//创建一个新意图
Intent intent = new Intent();
//设置意图要跳转的活动类
intent.setClass(this,Intent2Activity.class);
//往意图存入名叫request_time的字符串
intent.putExtra("request_time",DateUtil.getCurDateStr());
//往意图存入名叫request_content的字符串
intent.putExtra("request_content",et_response.getText().toString());
//期待接收下个页面的返回数据
startActivityForResult(intent,0);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (data != null){
//从意图中取出名叫response_time的字符串
String response_time = data.getStringExtra("response_time");
//从意图中取出名叫response_content的字符串
String response_content = data.getStringExtra("response_content");
String desc = String.format("收到返回消息:\n答应时间为%s\n应答内容为%s",response_time,response_content);
//把返回消息的详情显示在文本视图上
tv_response.setText(desc);
}
}
}
Main2Activity.java
package com.example.test_210715;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Intent2Activity extends AppCompatActivity implements View.OnClickListener {
private EditText et_response;//声明一个编辑框对象
private TextView tv_response;//声明一个文本视图对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent2);
findViewById(R.id.btn_transafer).setOnClickListener(this);
//从布局文件中获取名叫et_response的编辑框
et_response = findViewById(R.id.et_response);
//从布局文件中获取名叫tv_response的文本视图
tv_response = findViewById(R.id.tv_response);
//从前一个页面传来的意图中获取快递包裹
Bundle bundle = getIntent().getExtras();
//从包裹中取出名叫request_time的字符串
String request_time = bundle.getString("request_time");
//从包裹中取出名叫request_content的字符串
String request_content = bundle.getString("request_content");
String desc = String.format("收到请求信息:\n请求时间为%s\n请求内容为%s",request_time,request_content);
//把请求消息的详情显示在文本视图上
tv_response.setText(desc);
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_transafer){
Intent intent = new Intent();//创建一个新意图
Bundle bundle = new Bundle();//创建一个新包裹
//往包裹存放入名叫request_Time的字符串
bundle.putString("response_time",DateUtil.getCurDateStr());
//往包裹存入名叫respon_content的字符串
bundle.putString("response_content",et_response.getText().toString());
intent.putExtras(bundle);//把快递包裹给意图
setResult(Activity.RESULT_OK,intent);//携带意图返回前一个页面
finish();//关闭当前页面
}
}
}
工具类代码
DateUtil.java
package com.example.test_210715;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期操作工具类.
*
* @author shimiso
* http://blog.csdn.net/xuduzhoud/article/details/27526177
*/
public class DateUtil {
private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
public static Date str2Date(String str) {
return str2Date(str, null);
}
public static Date str2Date(String str, String format) {
if (str == null || str.length() == 0) {
return null;
}
if (format == null || format.length() == 0) {
format = FORMAT;
}
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
public static Calendar str2Calendar(String str) {
return str2Calendar(str, null);
}
public static Calendar str2Calendar(String str, String format) {
Date date = str2Date(str, format);
if (date == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
return date2Str(c, null);
}
public static String date2Str(Calendar c, String format) {
if (c == null) {
return null;
}
return date2Str(c.getTime(), format);
}
public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
return date2Str(d, null);
}
public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
if (d == null) {
return null;
}
if (format == null || format.length() == 0) {
format = FORMAT;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
String s = sdf.format(d);
return s;
}
public static String getCurDateStr() {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
+ c.get(Calendar.DAY_OF_MONTH) + "-"
+ c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)
+ ":" + c.get(Calendar.SECOND);
}
/**
* 获得当前日期的字符串格式
*
* @param format
* @return
*/
public static String getCurDateStr(String format) {
Calendar c = Calendar.getInstance();
return date2Str(c, format);
}
// 格式到秒
public static String getMillon(long time) {
return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);
}
// 格式到天
public static String getDay(long time) {
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
// 格式到毫秒
public static String getSMillon(long time) {
return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);
}
/*
输入的是String,格式诸如20120102,实现加一天的功能,返回的格式为String,诸如20120103
*/
public static String stringDatePlus(String row) throws ParseException {
String year=row.substring(0, 4);
String month=row.substring(4,6);
String day=row.substring(6);
String date1=year+"-"+month+"-"+day;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date startDate=sdf.parse(date1);
Calendar cd = Calendar.getInstance();
cd.setTime(startDate);
cd.add(Calendar.DATE, 1);
String dateStr =sdf.format(cd.getTime());
String year1=dateStr.substring(0,4);
String month1=dateStr.substring(5,7);
String day1=dateStr.substring(8);
return year1+month1+day1;
}
/*
输入的是String,格式诸如20120102,实现减一天的功能,返回的格式为String,诸如20120101
*/
public static String stringDateDecrease(String row) throws ParseException{
String year=row.substring(0, 4);
String month=row.substring(4,6);
String day=row.substring(6);
String date1=year+"-"+month+"-"+day;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date startDate=sdf.parse(date1);
Calendar cd = Calendar.getInstance();
cd.setTime(startDate);
cd.add(Calendar.DATE, -1);
String dateStr =sdf.format(cd.getTime());
String year1=dateStr.substring(0,4);
String month1=dateStr.substring(5,7);
String day1=dateStr.substring(8);
return year1+month1+day1;
}
/*
输入的格式为String,诸如20120101,返回的格式为String,诸如2012-01-01
*/
public static String stringDateChange(String date)
{
if(date.length()=="20120101".length()){
String year=date.substring(0, 4);
String month=date.substring(4,6);
String day=date.substring(6);
return year+"-"+month+"-"+day;
}else{
return date;
}
}
/**
* 日期向后推一天
* @param date 格式:20120101
* @return 20120102
*/
public static String tonextday(String date){
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6));
int day = Integer.parseInt(date.substring(6));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-1, day+1);
Date newdate = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String da = format.format(newdate);
return da;
}
/**
* 获取当前日期上一周的开始日期 (周日)
*/
public static String previousWeekByDate(String date) {
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6));
int day = Integer.parseInt(date.substring(6));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-1, day);
Date newdate = calendar.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(newdate);
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if(1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根据日历的规则,给当前日期减往星期几与一个星期第一天的差值
cal.add(Calendar.WEEK_OF_YEAR, -1);
String imptimeBegin = sdf.format(cal.getTime());
// System.out.println("所在周星期日的日期:"+imptimeBegin);
return imptimeBegin;
}
/**
* 获取当前日期上一周的结束日期 (周六)
*/
public static String previousWeekEndDayByDate(String date) {
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6));
int day = Integer.parseInt(date.substring(6));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-1, day);
Date newdate = calendar.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(newdate);
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if(1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s));
cal.add(Calendar.WEEK_OF_YEAR, -1);
String imptimeBegin = sdf.format(cal.getTime());
// System.out.println("星期六的日期:"+imptimeBegin);
return imptimeBegin;
}
/**
* 获取当前日期当前一周的开始日期 (周日)
*/
public static String getCurrentWeekFirstDayByDate(String date) {
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6));
int day = Integer.parseInt(date.substring(6));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-1, day);
Date newdate = calendar.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(newdate);
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if(1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根据日历的规则,给当前日期减往星期几与一个星期第一天的差值
String imptimeBegin = sdf.format(cal.getTime());
// System.out.println("所在周星期日的日期:"+imptimeBegin);
return imptimeBegin;
}
/**
* 获取当前日期当前一周的结束日期 (周六)
*/
public static String getCurrentWeekEndDayByDate(String date) {
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6));
int day = Integer.parseInt(date.substring(6));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-1, day);
Date newdate = calendar.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(newdate);
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
if(1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s));
String imptimeBegin = sdf.format(cal.getTime());
return imptimeBegin;
}
public static String previousMonthByDate(String date) {
// TODO Auto-generated method stub
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6));
int day = Integer.parseInt(date.substring(6));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-2, 1);
Date newdate = calendar.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(newdate);
String imptimeBegin = sdf.format(cal.getTime());
// System.out.println(imptimeBegin);
return imptimeBegin;
}
public static String nextMonthByDate(String date) {
// TODO Auto-generated method stub
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6));
int day = Integer.parseInt(date.substring(6));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, 1);
Date newdate = calendar.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(newdate);
String imptimeBegin = sdf.format(cal.getTime());
// System.out.println(imptimeBegin);
return imptimeBegin;
}
public static String getCurrentMonthFirstDayByDate(String date) {
int year = Integer.parseInt(date.substring(0,4));
int month = Integer.parseInt(date.substring(4,6));
int day = Integer.parseInt(date.substring(6));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-1, 1);
Date newdate = calendar.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(newdate);
String imptimeBegin = sdf.format(cal.getTime());
return imptimeBegin;
}
}
如果你觉得我这篇文章不错或者帮助到了你可以在本篇文章中给我点一个赞,您的一个小小鼓励将会是我在敲码路上的大大动力!!!!
上一篇: Android开发中的小知识
下一篇: JAVA输入输出(IO)之文件