Activity2.java

package com.hanqi.test4;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText; public class Activity2 extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2); //接收信息
//获取意图 //传递过来的Intent
Intent intent = getIntent(); String s = intent.getExtras().getString("myet");
EditText mytv = (EditText)findViewById(R.id.mytv);
mytv.setText(s);
}
//普通返回
public void onclick(View v)
{
//关闭当前activity
finish();
}
public void onclick2(View v)
{
//存储返回数据 也要用intent EditText mytv = (EditText)findViewById(R.id.mytv);
// Log.e("TAG", "mytv =" + mytv.getText().toString());
//
Bundle bundle = new Bundle();
bundle.putString("mytv", mytv.getText().toString());
//设置返回数据
//先设置ResultCode,再设置存储数据的意图
setResult(RESULT_OK, new Intent().putExtras(bundle)); finish(); }
}

activity_2.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="com.hanqi.test4.Activity2"> <EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="测试"
android:id="@+id/mytv"
/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通返回"
android:onClick="onclick"
/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带数据返回"
android:onClick="onclick2"
/> </LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqi.test4"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter> <!--//意图过滤器-->
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name=".Activity2"></activity>
</application> </manifest>

MainActivity.java

package com.hanqi.test4;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; /**
* Created by Administrator on 2016/3/21.
*/
public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main_layout);
} //普通方式
public void onClick(View v)
{
Log.e("Test4_TAG", "按钮的点击监听被触发");
//静态方法
//特点:不用实例化/直接用类名就可以调用
//构建了Toast实例
//方法链(一个方法后面跟另一个方法)
Toast.makeText(this,"按钮的点击监听被触发",Toast.LENGTH_LONG).show(); // Toast toast = Toast.makeText(this, "按钮的点击被触发", Toast.LENGTH_LONG);
// toast.show();
//意图 intent
//取得要传递的信息
//获取view实例 EditText myet = (EditText)findViewById(R.id.myet);
String string = myet.getText().toString();
Intent intent = new Intent(this,Activity2.class);
//存储内容
//getExtra Bundle 实际是一个HashMap,进行限制
//intent.getExtras().putString("myet",string); intent.putExtra("myet",string);//重载 //启动方法不一样
startActivity(intent); } //带返回的方式 public void onclick2(View v)
{
EditText myet = (EditText)findViewById(R.id.myet);
String string = myet.getText().toString();
Intent intent = new Intent(this,Activity2.class); intent.putExtra("myet", string); //有返回数据的启动方式
//第一个参数是intent
//第二个参数是requestCode(请求码)作用:为了区分不同的请求
startActivityForResult(intent,1); } //重写 处理返回信息的监听(也叫回调方法)
//监听所有返回信息的
//必须要有requestCode区分由哪个请求返回的 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("TAG","requestCode="+requestCode+"resultCode"+resultCode); if(requestCode ==1)
{
if (resultCode ==RESULT_OK)
{
//获取返回信息
String string = data.getExtras().getString("mytv");
EditText editText = (EditText)findViewById(R.id.myet);
editText.setText(string);
Toast.makeText(this, "返回信息 =" + string, Toast.LENGTH_LONG);
}
else
{
Toast.makeText(this,"返回信息有误",Toast.LENGTH_SHORT);
}
}
} }

main_layout.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"
> <EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/myet"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通方式"
android:onClick="onClick"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带返回方式"
android:onClick="onclick2"/>
</LinearLayout>

Android课程---Activity 带返回值的跳转的更多相关文章

  1. 【Android】12.3 在当前Activity中获取另一个Activity的返回值

    分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 在上一节的示例中,通过StartActivity(Intent)方法启动另一个Activity后,这两个Activ ...

  2. 064 01 Android 零基础入门 01 Java基础语法 08 Java方法 02 无参带返回值方法

    064 01 Android 零基础入门 01 Java基础语法 08 Java方法 02 无参带返回值方法 本文知识点:无参带返回值方法 说明:因为时间紧张,本人写博客过程中只是对知识点的关键步骤进 ...

  3. 慕课网-Java入门第一季-7-3 Java 中无参带返回值方法的使用

    来源:http://www.imooc.com/code/1579 如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值 ...

  4. Java 中无参带返回值方法的使用

    如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值为 int 类型的方法,执行的操作为计算两数之和,并返回结果 在 c ...

  5. 13 继续C#中的方法,带返回值的方法介绍

    在这一个练习中,我们要使用带返回值的方法.如果一个方法带返回值,那么它的形式是这样的. 定义一个带返回值的C#方法 static 返回类型 方法名字 (参数类型 参数1的名字,参数类型 参数2的名字) ...

  6. activity 接回返回值

    activity 接回返回值 今天做订单列表显示 点击某一项显示订单详细信息,在详细activity中用户可以选择取消订单(未支付的状态下)当用户取消订单后订单列表也要改变状态,原来最初做法是所加载绑 ...

  7. Java 中带参带返回值方法的使用

    如果方法既包含参数,又带有返回值,我们称为带参带返回值的方法. 例如:下面的代码,定义了一个 show 方法,带有一个参数 name ,方法执行后返回一个 String 类型的结果 调用带参带返回值的 ...

  8. EF5中 执行 sql语句使用Database.ExecuteSqlCommand 返回影响的行数 ; EF5执行sql查询语句 Database.SqlQuery 带返回值

    一: 执行sql语句,返回受影响的行数 在mysql里面,如果没有影响,那么返回行数为  -1 ,sqlserver 里面  还没有测试过 using (var ctx = new MyDbConte ...

  9. 测试 多线程 实现 callable 带返回值

    package threadTest; import java.util.ArrayList; import java.util.Date; import java.util.concurrent.C ...

随机推荐

  1. 【虚拟机】苹果虚拟机mac10.11.6+Xcode8.1

    [虚拟机]苹果虚拟机mac10.11.6+Xcode8.1本虚拟机加装Xcode8.1,方便大家更好学习Swift3.0语言以及iOS开发.安装注意事项:第一步:确认硬件:1.确认主板以及cpu支持虚 ...

  2. linux(centos6)搭建ftp服务器

    前提 ssh服务已经开启,关闭防火墙,主机和虚拟机能ping通 查看ssh和防火墙的状态 service sshd status service iptables status 开启ssh服务 ser ...

  3. [SDOI2016]部分题选做

    听说SDOI蛮简单的,但是SD蛮强的.. 之所以是选做,是因为自己某些知识水平还不到位,而且目前联赛在即,不好花时间去学sa啊之类的.. bzoj4513储能表&bzoj4514数字配对 已写 ...

  4. https://www.nginx.com/blog/introduction-to-microservices/

    https://www.nginx.com/blog/introduction-to-microservices/

  5. 使用Spring发送邮件

    http://www.oschina.net/code/snippet_253813_36503

  6. iOS socket TCP UDP

    TCP: 服务器: #import <Foundation/Foundation.h> #include <sys/socket.h> #include <netinet ...

  7. 通过adb命令打印log

    1.adb logcat  --打印当前设备上所有日志 2.adb logcat | findstr *** --过滤仅含***的日志 3.adb logcat *:W  --过滤打印严重级别W及以上 ...

  8. 请将 php.ini 中的 short_open_tag 设置为 On,否则无法继续安装。

    安装的wamp套件,访问http://localhost/Discuz/install/index.PHP进行安装操作,提示 对不起,请将 php.ini 中的 short_open_tag 设置为 ...

  9. Codeforces 633C Spy Syndrome 2(DP + Trie树)

    题目大概说给一个加密的字符串,加密规则是把原文转化成小写字母,然后各个单词反转,最后去掉空格.现在给几个已知的单词,还原加密的字符串. 和UVa1401一个道理.. 用dp[i]表示加密字符前i个字符 ...

  10. 【转】vim格式化C代码

    转自:http://blog.chinaunix.net/uid-24774106-id-3396220.html 在自己的目录下编辑自己的.vimrc, vim ~/.vimrc 添加下面的几行: ...