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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"></activity>
</application> </manifest>

MainActivity

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("T4TAG","按钮的点击监听被触发");
//静态方法
//直接用类名就可以调用,不需要实例化
//构建了一个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,Main2Activity.class);
//存储内容
//getExtra Bundle 实际是一个HashMap 进行了限制
//intent.getExtras().putString("myet",string);
intent.putExtra("myet",string); startActivity(intent);
}
//带返回的方式 public void onCLICK(View v)
{
EditText myet=(EditText)findViewById(R.id.myet); String string= myet.getText().toString(); Intent intent= new Intent(this,Main2Activity.class);
//存储内容
//getExtra Bundle 实际是一个HashMap 进行了限制
//intent.getExtras().putString("myet",string);
intent.putExtra("myet",string);
//有返回数据的启动方式
//第一个参数 intent
//第二个参数 requestCode 请求码
startActivityForResult(intent, 1);
}
//重写 处理返回信息的监听(回调方法)
//onActivityResult通用监听 监听所有返回信息的
//必须要有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"> <EditText
android:layout_width="100dp"
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="onCLICK"
/>
</LinearLayout>

Main2Activity

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 Main2Activity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); //接受信息
//获取意图
//传递过来的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 ONclock(View v)
{
//存储返回数据 也要用intent
EditText mytv=(EditText)findViewById(R.id.mytv);
Bundle bundle =new Bundle();
bundle.putString("mytv",mytv.getText().toString()); //设置返回数据
// 先设置ReaultCode,再设置存储数据的意图
setResult(RESULT_OK,new Intent().putExtra("mytv",mytv.getText().toString()));
//关闭当前activity
finish();
}
}

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="com.hanqi.test4.Main2Activity"> <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="ONclock"
/>
</LinearLayout>

android--Activity有返回值的跳转的更多相关文章

  1. Android课程---Activity 带返回值的跳转

    Activity2.java package com.hanqi.test4; import android.content.Intent; import android.os.Bundle; imp ...

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

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

  3. activity 接回返回值

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

  4. android 16 带返回值的activity

    main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...

  5. Android - 和其他APP交互 - 获得activity的返回值

    启用另一个activity不一定是单向的.也可以启用另一个activity并且获得返回值.要获得返回值的话,调用startActivityForResult()(而不是startActivity()) ...

  6. Android checkCallingPermission()方法返回值问题

    Android开发检查权限时,发现调用checkCallingPermission()总是返回值-1,而Binder.getCallingPid() == Process.myPid()又总是返回tr ...

  7. [android] setOnTouchEvent 设置返回值为true 和 false的区别

    今天在做自定义的可选文本的 TextView 类时,用到了 View 类的 setOnTouchListener(OnTouchListener l)事件监听,在构造 OnTouchListener ...

  8. phonegap android插件,启动activity并返回值

    Your execute menthod is not quite right. When you do: return new PluginResult(PluginResult.Status.OK ...

  9. 【Python web自动化】之读取配置文件参数,利用cookie返回值进行跳过验证码进行登录操作

    当进行Python的Web自动化时,会涉及到验证码问题,该如何跳过执行呢,下面请看代码: 1.首先新建配置文件*.ini格式 config.ini [db] #基础地址: baseurl = http ...

随机推荐

  1. repo 原理

    Android源代码工程用repo来进行管理,本质是多个git仓的整合. 感谢https://blog.csdn.net/stoic163/article/details/78790349 1.Gen ...

  2. vue-router,vuex

    vue设置路由为了服务器渲染今天换另一种方式首先在文件夹中router建立router和routes两个js文件,router用来设置路由,routes用来建立路由代码如下: router: impo ...

  3. C++字符串和向量

    陷阱:C字符串使用=和== char a_string[10]; a_string="Hello"  非法 strcpy(a_string,"Hello");  ...

  4. requests库的文档--快速上手

    快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...

  5. Python 图示集绵

    http://nbviewer.jupyter.org/github/pyecharts/pyecharts-users-cases/blob/master/notebook-users-cases/ ...

  6. java分解质因数,具体程序分析和代码

    题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5. 将一个正整数分解质因数分析:对n进行分解质因数,找到最小的质数k如果这个质数恰好等于n则说明分解质因数过程已经结束,打印输出 ...

  7. Activity 与 Task

    [Activity 与 Task] A task is a collection of activities that users interact with when performing a ce ...

  8. Avalon Framework

    Apache Avalon has closed. Apache Avalon began in 1999 as the Java Apache Server Framework and in lat ...

  9. char、varchar、nchar、nvarchar的区别(转)

    转自:http://tech.diannaodian.com/dw/data/sql/2011/1005/135556.html 对于程序中的string型字段,SQLServer中有char.var ...

  10. FPGA基础知识1

    1.乘法 在FPGA中,乘法运算可以分为 1)信号与信号之间的运算,用乘法器核实现: 2)常数与信号之间的运算,利用移位及加减法实现. A x 16 = A左移4位: A x 20 = A x 16  ...