一个实例弄明白startActivityForResult和intent怎么使用

2024-02-11 09:38

本文主要是介绍一个实例弄明白startActivityForResult和intent怎么使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录(?)[+]

一、Intent实现Activity之间的切换

1、构造函数法:

[java]  view plain copy
print ?
  1. Intent intent = new Intent(this , OtherActivity.class); //两个参数:第一个是上下文对象,第二个是要切换的Activity的class字节码  
  2. startActivity(intent);   

2、setClass方法:

[java]  view plain copy
print ?
  1. Intent intent = new Intent();  
  2. intent.setClass(this, OtherActivity.class); //设置要激活的组件  
  3. startActivity(intent);        

3、setClassName方法:

[java]  view plain copy
print ?
  1. Intent intent = new Intent();  
  2. intent.setClassName(this"cn.itcast.activitys.OtherActivity");  
  3. startActivity(intent);  

4、setComponent方法:

[java]  view plain copy
print ?
  1. Intent intent = new Intent();  
  2. intent.setComponent(new ComponentName(this,OtherActivity.class));  
  3. startActivity(intent);  

二、startActivityForResult用来传递参数

首先,我们是定义了两个Activity,就是两个界面:A和B。

A界面包含一个按钮:登陆

B界面包含两个输入框和一个登陆按钮:用户名和密码输入框、登陆按钮。

要求,从A界面传递一个字符串,B界面能够显示。

B界面输入的用户名和密码,在点击登陆后,可以在A界面显示。


首先,我们看下A界面的界面:

[html]  view plain copy
print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <Button   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/button"  
  11.         android:onClick="openActivity"  
  12.         />  
  13.       
  14. </LinearLayout>  
上边包含一个按钮,这个按钮定义了一个onClick属性,设置了点击方法:openActivity


然后,我们编写A界面的Java调用代码(MainActivity.java):

[java]  view plain copy
print ?
  1. package cn.itcast.activitys;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Toast;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.     }  
  17.       
  18.     public void openActivity(View v){ //在  
  19.         Intent intent = new Intent(this , OtherActivity.class);  
  20.           
  21.         Bundle bundle = new Bundle();  
  22.         bundle.putString("result""我是从MainActivity传递过来的参数");  
  23.         intent.putExtras(bundle);  
  24.           
  25.         startActivityForResult(intent, 200); //两个参数:第一个是意图对象,第二个是请求码requestCode  
  26.     }  
  27.   
  28.     @Override  
  29.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  30.         String name="",password="";  
  31.         if(resultCode == 30){  //判断返回码是否是30  
  32.             name = data.getStringExtra("name").toString();  
  33.             password = data.getStringExtra("password").toString();  
  34.             Toast.makeText(this"您登陆的用户名是:"+name+",密码是:"+password, 1).show();  
  35.         }     
  36.         super.onActivityResult(requestCode, resultCode, data);        
  37.     }  
  38.       
  39. }  
在openActivity(View v)这个方法中,我们定义了一个Bundle对象,然后传入一个名为“result”的属性参数,然后放在intent对象中,通过startActivityForResult(Intent intent,int requestCode)这个方法进行传递。可以看出我们传递到了OtherActivity.java这个文件了


然后,我们要先看下OtherActivity.java里边的代码:

[java]  view plain copy
print ?
  1. package cn.itcast.activitys;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9. import android.widget.Toast;  
  10.   
  11. public class OtherActivity extends Activity {  
  12.     private EditText name;  
  13.     private EditText password;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.otheractivity);  
  18.           
  19.         Intent intent = getIntent(); //用于激活它的意图对象:这里的intent获得的是上个Activity传递的intent  
  20.         Bundle bundle = intent.getExtras();  
  21.         String result = bundle.getString("result");  
  22.         Toast.makeText(this, result, 1).show();  
  23.     }  
  24.       
  25.     public void closeActivity(View v){  
  26.         Intent data = new Intent();  
  27.         name = (EditText)this.findViewById(R.id.name);  
  28.         password = (EditText)this.findViewById(R.id.password);  
  29.       
  30.         data.putExtra("name", name.getText().toString());  
  31.         data.putExtra("password", password.getText().toString());  
  32.         setResult(30, data); //设置返回数据   
  33.         this.finish(); //关闭当前Activity     
  34.           
  35.     }  
  36. }  
在onCreae()方法中,我们调用Toast对象把获得的参数在界面中显示出来。

然后我们调用otheractivity.xml界面中的按钮的onClick方法来调用closeActivity()方法。

通过这个方法,我们可以把文本框中输入的用户名和密码通过setResult(int resultCode,Intent intent)方法传递回A界面,调用this.finish()关闭当前界面。


接下来,我们看下B界面的源码:otheractivity.xml

[html]  view plain copy
print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView   
  8.         android:layout_width="fill_parent"  
  9.          android:layout_height="wrap_content"  
  10.          android:text="@string/name"  
  11.         />  
  12.     <EditText   
  13.          android:layout_width="fill_parent"  
  14.          android:layout_height="wrap_content"  
  15.         android:id="@+id/name"  
  16.         />  
  17.      <TextView   
  18.         android:layout_width="fill_parent"  
  19.          android:layout_height="wrap_content"  
  20.          android:text="@string/password"  
  21.         />  
  22.      <EditText   
  23.          android:layout_width="fill_parent"  
  24.          android:layout_height="wrap_content"  
  25.         android:id="@+id/password"  
  26.         />  
  27.     <Button   
  28.         android:layout_width="fill_parent"  
  29.         android:layout_height="wrap_content"  
  30.         android:text="@string/closebutton"  
  31.         android:onClick="closeActivity"  
  32.         />  
  33. </LinearLayout>  


当调用this.finish()方法后,B界面结束,返回A界面。

我们可以看到,在MainActivity.java中有一个重写的方法:onActivityResult()。这个方法是系统提供的,可以在“右键—>source—>Override/Implement Methods”中找到这个方法并重写。

这个方法在setResult()返回后调用,我们在其中判断返回码是否是在OtherActivity.java中传递过来的30,如果是的话,就把用户名和密码用Toast对象显示出来。

这篇关于一个实例弄明白startActivityForResult和intent怎么使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/699452

相关文章

Mysql中RelayLog中继日志的使用

《Mysql中RelayLog中继日志的使用》MySQLRelayLog中继日志是主从复制架构中的核心组件,负责将从主库获取的Binlog事件暂存并应用到从库,本文就来详细的介绍一下RelayLog中... 目录一、什么是 Relay Log(中继日志)二、Relay Log 的工作流程三、Relay Lo

使用Redis实现会话管理的示例代码

《使用Redis实现会话管理的示例代码》文章介绍了如何使用Redis实现会话管理,包括会话的创建、读取、更新和删除操作,通过设置会话超时时间并重置,可以确保会话在用户持续活动期间不会过期,此外,展示了... 目录1. 会话管理的基本概念2. 使用Redis实现会话管理2.1 引入依赖2.2 会话管理基本操作

Springboot请求和响应相关注解及使用场景分析

《Springboot请求和响应相关注解及使用场景分析》本文介绍了SpringBoot中用于处理HTTP请求和构建HTTP响应的常用注解,包括@RequestMapping、@RequestParam... 目录1. 请求处理注解@RequestMapping@GetMapping, @PostMappin

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

Nginx服务器部署详细代码实例

《Nginx服务器部署详细代码实例》Nginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务,:本文主要介绍Nginx服务器部署的相关资料,文中通过代码... 目录Nginx 服务器SSL/TLS 配置动态脚本反向代理总结Nginx 服务器Nginx是一个‌高性

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

Python中Request的安装以及简单的使用方法图文教程

《Python中Request的安装以及简单的使用方法图文教程》python里的request库经常被用于进行网络爬虫,想要学习网络爬虫的同学必须得安装request这个第三方库,:本文主要介绍P... 目录1.Requests 安装cmd 窗口安装为pycharm安装在pycharm设置中为项目安装req

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

Python使用Matplotlib和Seaborn绘制常用图表的技巧

《Python使用Matplotlib和Seaborn绘制常用图表的技巧》Python作为数据科学领域的明星语言,拥有强大且丰富的可视化库,其中最著名的莫过于Matplotlib和Seaborn,本篇... 目录1. 引言:数据可视化的力量2. 前置知识与环境准备2.1. 必备知识2.2. 安装所需库2.3