android入门:activity之间跳转,并且回传参数
介绍:
<?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="horizontal">
<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/send_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"
/>
<!--定义onclick函数-->
</LinearLayout>
package com.example.administrator.helloworld;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
//发送Intent对应字符串内容的key
public static final String Intent_key="MESSAGE";
//EditText
private EditText editText =null;
private void initView(){
editText= (EditText) findViewById(R.id.edit_message);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置布局
setContentView(R.layout.activity_main);
initView();
}
//发送消息,启动secondActivity!
public void sendMessage(View view){
Intent intent = new Intent(this,secondActivity.class);
String text =editText.getText().toString();
intent.putExtra(Intent_key,text);
startActivityForResult(intent,0);//此处的requestCode应与下面结果处理函中调用的requestCode一致
}
//结果处理函数,当从secondActivity中返回时调用此函数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0 && resultCode==RESULT_OK){
Bundle bundle = data.getExtras();
String text =null;
if(bundle!=null)
text=bundle.getString("second");
Log.d("text",text);
editText.setText(text);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="50dp"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="点击按钮返回"
/>
</LinearLayout>
package com.example.administrator.helloworld;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class secondActivity extends AppCompatActivity {
private Button button=null;
private TextView textView =null;
//设置类ButtonListener实现接口,OnClickListener,在其中可以指定不同id的button对应不同的点击事件
//可以借此将代码抽出来,提高代码的可阅读性
private class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
Intent intent =getIntent();
//这里使用bundle绷带来传输数据
Bundle bundle =new Bundle();
//传输的内容仍然是键值对的形式
bundle.putString("second","hello world from secondActivity!");//回发的消息,hello world from secondActivity!
intent.putExtras(bundle);
setResult(RESULT_OK,intent);
finish();
break;
}
}
}
//初始化View
public void initView(){
button= (Button) findViewById(R.id.button);
textView= (TextView) findViewById(R.id.textView);
button.setOnClickListener(
new ButtonListener()
);
Intent intent =getIntent();
String text =intent.getStringExtra(MainActivity.Intent_key);
textView.setText(text);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
initView();
}
}

android入门:activity之间跳转,并且回传参数的更多相关文章
- Android之Activity之间跳转
本人自学Android,想到什么就写点什么.主要是怕忘了,哈哈~请观者不要建议~ 今天写点Android窗口之间的跳转以及自己理解: 1.Android中窗口之间的跳转,就是Activity之间的跳转 ...
- Android:Activity之间跳转和参数传递
一个activity就好比一个网页,此文章讲解怎样创建一个activity并且实现跳转! 一.学习创建Activity 1.新建一个java类,右击src目录,选择new-->class,新的a ...
- android入门——Activity(2)
主要内容:一.IntentFlag 二.简单复杂数据传递 三.数据回传 四.打开系统界面 五.IntentFilter匹配 一.IntentFlag 复制一段内容 来源 http://i ...
- 从Android中Activity之间的通信说开来[转]
http://www.cnblogs.com/virusswb/archive/2011/08/02/2124824.html 引言 最近两个星期在研究android的应用开发,学习了android应 ...
- Android中Activity之间的数据传递
在开发中,我们经常涌用到Activity,那么既然用到了Activity,就一定免不了在两个或者多个Activity之间传递数据.这里我们先说一说原理,然后在看看代码和例子. 情况A:我们需要从Act ...
- Android之Activity之间传递对象
在非常多时候,我们须要在Activity之间传递对象,比方当你点击了某列表的item,须要传递给下一个Activity该对象,那我们须要该怎么做呢? Android支持两种传递对象的方式.一种是bun ...
- 大叔也说Xamarin~Android篇~Activity之间传递数组
回到目录 我们在开发应用程序时,不可能只使用一个Layout或者一个Activity,比如你个管理系统,要求用户先登陆然后再使用,这时你至少要有两个activity吧,先登陆一个,然后成功后需要跳到别 ...
- Android课程---Activity的跳转与传值(转自网上)
Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据. Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...
- android入门——Activity(1)
结构图 mainfests文件夹下面有一个AndroidMainfest.xml文件,类似web开发中的web.xml文件负责整个项目的配置,每当我们新建一个activity,都要在这个文件中进行配置 ...
随机推荐
- 基于JAVA语言的多线程技术
1.简介 多线程技术属于操作系统范围内的知识: 进程与线程 可以这么理解,一个应用程序就是一个进程,在一个进程中包含至少一个线程:进程就是线程的容器,真正工作.处理任务的是线程. 进程是操作系统分配资 ...
- C# 经典入门15章 -ListView 【未附代码】
- Warning: The Copy Bundle Resources build phase contains this target's Info.plist file 'yintingting_baisi/Info.plist'.
处理方法: The INFOPLIST_FILE build setting specifies the name of the Info.plist associated with your tar ...
- 黄聪:基于Asp.net的CMS系统We7架设实验(环境WIN7,SQL2005,.NET3.5)(初学者参考贴)
http://www.cnblogs.com/huangcong/archive/2010/03/30/1700348.html
- Mac os 10.11 更新ruby
1.装cocoapods,ruby版本忒低->开始更新ruby->开始更新gem,这是一条不归路啊同志们,各种permission denied,各种路径不存在,各种路径没有读写权限,各种 ...
- 转:sql SELECT时的with(nolock)选项说明
I used to see my senior developers use WITH (NOLOCK) when querying in SQL Server and wonder why they ...
- 在java中使用dom4j包对String格式的xm数据l解析
在网上找了好久,都没搞出来,借鉴别人的代码,依葫芦画瓢,写了个自己用的解析类.注意节点属性和子节点的区别就好了,这个包的方法还挺好用的 package com.allinpay.utils; impo ...
- Android网络开发之Volley--Volley基本用法StringRequest(一)
1.StringRequest用法 主要分为3步: (1).实例化一个RequestQueue对象 (2).设置StringRequest对象参数,并将StringRequest对象加入Request ...
- STM8单片机启动流程彻底探究--基于IAR开发环境
初学STM8会发现,STM8官方的固件库并没有提供一个.s文件的启动代码,那么她是如何启动然后跳转到main函数执行的呢 首先,我们根据ARM的只是可以推测,STM8也是通过复位向量来启动的,假设流程 ...
- Hibernate---第一个helloworld程序 (XML版本, annotation版本)
Hibernate作为JPA的一种实现,jpa的注解已经是hibernate的核心,hibernate只提供了一些补充,而不是两套注解.hibernate对jpa的支持够足量,在使用hibernate ...