应用场景:

在Android的开发过程中,有时需要进行Activity之间的跳转,在页面跳转的过程中,有时需要传递指定的参数数据传过去,例如:信息发布app返回查询条件的需要,为此,就需要了解这方面的技术与实现方式。

知识点介绍:

在Android中Intent的作用,可以实现一个Activity到另一个Activity的跳转,但其功能并不只局限于这一点。
在Android中startActivityForResult主要作用:
A-Activity需要在B-Activtiy中执行一些数据操作,而B-Activity又要将执行后的操作数据的结果返回给A-Activtiy。

使用方式:

第一步:新建一个Android项目PageJumpTest

第二步:新建两个Activity: FirstActivity,SecondActivity。

【FirstActivity.java】

package com.example.pagejumptest;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class FirstActivity extends Activity {private TextView textView;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_first);initViews();}private void initViews() {textView = (TextView) findViewById(R.id.first_textview);button = (Button) findViewById(R.id.first_button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//从FirstActivity跳转到SecondActivity的Intent(意图)Intent intent = new Intent(FirstActivity.this, SecondActivity.class);//执行Intent ★使用startActivityForResult来启动  startActivityForResult(intent, 0);}});}/** * 复写onActivityResult方法 * 当SecondActivity页面关闭时,接收SecondActiviy页面传递过来的数据。 */@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {Bundle bundle = data.getExtras();String param = bundle.getString("param");textView.setText(param);}}
【activity_first.xml】

<RelativeLayout 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"    android:background="#FFFFFF"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".FirstActivity" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <TextView            android:id="@+id/first_textview"            android:layout_width="match_parent"            android:layout_height="60dp"            android:gravity="center"            android:hint="获取SecondActivity返回数据"            android:textColor="#CCCCFF" />        <Button            android:id="@+id/first_button"            android:layout_width="match_parent"            android:layout_height="60dp"            android:background="#CCCCFF"            android:text="跳转到SecondActivity"            android:textSize="20dp" />    </LinearLayout></RelativeLayout>

【SecondActivity.java】

package com.example.pagejumptest;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class SecondActivity extends Activity {private EditText editText ;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);initViews();}private void initViews() {editText = (EditText) findViewById(R.id.second_textview);button = (Button) findViewById(R.id.second_button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//获取Intent,添加需要返回的数据,返回Intent,关闭本页Intent intent = getIntent();intent.putExtra("param", editText.getText().toString());setResult(0, intent);SecondActivity.this.finish();}});}}

【activity_second.xml】

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:background="#FFFFFF"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".SecondActivity" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <EditText            android:id="@+id/second_textview"            android:layout_width="match_parent"            android:layout_height="60dp"            android:gravity="center"            android:hint="向FirstActivity返回的数据"            android:textColor="#99CC66" />        <Button            android:id="@+id/second_button"            android:layout_width="match_parent"            android:layout_height="60dp"            android:background="#99CC66"            android:text="关闭SecondActivity页面"            android:textSize="20dp" />    </LinearLayout></RelativeLayout>

下载地址:

http://download.csdn.net/download/ma_hoking/7270809


页面效果:效果截图




更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. 我和我的Android
  4. [置顶] android adapter
  5. Android中使用Notification
  6. 【android】android Handler应用详解
  7. Android主流三方库源码分析(四、深入理解GreenDao源码)
  8. android的URI学习
  9. Android(安卓)开发手记一 NDK编程实例

随机推荐

  1. android listview继承BaseAdapter,自定义
  2. EditText支持Search按键搜索
  3. Android上的单元测试
  4. Android ImageView配置android:layout_he
  5. 设置Button挨在一起
  6. js调用android的版本兼容问题
  7. android用okhttp和retrofit访问网络的时
  8. 转载:Android中针对怎么来使用
  9. AndroidX之CoordinatorLayout+AppBarLayo
  10. Android开发历程之三