首页 > 其他分享 >Android学习之Intent接收返回数据

Android学习之Intent接收返回数据

时间:2023-03-17 13:33:04浏览次数:37  
标签:layout id content Intent import Android 接收 android


send_main.xml:




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按我将跳转到另一个Activity程序"/>
<TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>




 


 

 

receive_main.xml:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/retbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回数据到Send。"/>
</LinearLayout>



 


 

 

strings.xml:



<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_title">Intent操作</string>
<string name="send_name">发送Intent的Activity程序。</string>
<string name="receive_name">接收Intent的Activity程序。</string>
</resources>




 


 

 

AndroidManifest.xml:



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.demo" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application android:icon="@drawable/icon" android:label="@string/app_title">
<activity android:name="Send" android:label="@string/app_title">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Receive" android:label="@string/receive_name" />
</application>
</manifest>


 


 

 

Send.java:


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Send extends Activity {
private Button mybut = null ; // 按钮组件
private TextView msg = null ; // 文本组件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.send_main); // 默认布局管理器
this.mybut = (Button) super.findViewById(R.id.mybut) ; // 取得组件
this.msg = (TextView) super.findViewById(R.id.msg) ; // 取得组件
this.mybut.setOnClickListener(new OnClickListenerImpl()); // 定义单击事件
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Intent it = new Intent(Send.this, Receive.class); // 实例化Intent
it.putExtra("myinfo", "我的发送信息测试") ; // 设置附加信息
Send.this.startActivityForResult(it, 1); // 启动Activity
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) { // 判断操作类型
case RESULT_OK: // 成功操作
msg.setText("返回的内容是:" + data.getStringExtra("retmsg"));
break;
case RESULT_CANCELED: // 取消操作
msg.setText("操作取消。");
break ;
default:
break;
}
}
}


 


 

 

Receive.java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Receive extends Activity {
private TextView show = null ; // 文本显示组件
private Button retbut = null ; // 按钮组件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.receive_main); // 调用默认布局管理器
this.show = (TextView) super.findViewById(R.id.show) ; // 取得组件
this.retbut = (Button) super.findViewById(R.id.retbut) ;// 取得组件
Intent it = super.getIntent() ; // 取得启动此程序的Intent
String info = it.getStringExtra("myinfo") ; // 取得设置的附加信息
this.show.setText(info) ; // 设置文本显示信息
this.retbut.setOnClickListener(new OnClickListenerImpl()) ; // 设置监听
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View view) {
Receive.this.getIntent().putExtra("retmsg", "我的返回信息测试") ;// 返回信息
// 设置返回数据的状态,RESULT_OK与Send.java中的onActivityResult()里判断的对应
Receive.this.setResult(RESULT_OK, Receive.this.getIntent()) ;
Receive.this.finish() ; // 结束Intent
}
}
}

 

标签:layout,id,content,Intent,import,Android,接收,android
From: https://blog.51cto.com/u_4427045/6127117

相关文章

  • 【Android 逆向】【攻防世界】人民的名义-抓捕赵德汉1-200
    1.这一题下载下来是个jar文件,感觉很android关系不大,但还是放在了mobile这个分类下了2.直接javajar运行,提示需要输入密码#java-jar169e139f152e45d5ae634223fe53e6b......
  • 【Android 逆向】【攻防世界】APK逆向
    1.apk安装到手机,提示输入flag2.jadx打开apk定位到checkSN方法publicbooleancheckSN(StringuserName,Stringsn){if(userName!=null){try{......
  • Android轻量级数据SparseArray详解
    SparseArray是Android中特有的数据结构,他的几个重要的特点;以键值对形式进行存储,基于二分查找,因此查找的时间复杂度为0(LogN);.由于SparseArray中Key存储的是数组形式,......
  • Android Studio预览Markdown文件办法
     01、安装Markdown插件打开File>>Settings>>Plugins>>Marketplace菜单,输入Markdown搜索,点击Install安装。02、下载ChooseRuntime插件JetBrains插件市场......
  • Unity 学习使用InputSystem接收键盘的输入
    目录快速实践配置InputAction创建场景编写代码理论学习ActionInputActionTypeInputBinding快速实践配置InputAction右键点击工程(project)面板空白处,弹出菜单栏,选择Cr......
  • android studio之常用基本控件的使用
    转自:(7条消息)androidstudio之常用基本控件的使用_androidstudio控件栏_PPYY3344的博客-CSDN博客在Android开发中,需要使用的控件很多,除了TextView、Button、EditText......
  • Android android:exported="true" 属性
    android:exported="true"是什么android:exported其实并不是Android12的新属性,在前面的版本也可以看见它。它是Android中的四大组件Activity,Service,Provider,Receiver四......
  • 【Android 逆向】【攻防世界】android2.0
    这是一道纯算法还原题1.apk安装到手机,提示输入flag,看来输入就是flag2.jadx打开apk查看this.button.setOnClickListener(newView.OnClickListener(){//fr......
  • Android代码静态检查(lint、Checkstyle、ktlint、Detekt)
    Android代码静态检查(lint、Checkstyle、ktlint、Detekt)在​​Android​​项目开发过程中,开发团队往往要花费大量的时间和精力发现并修改代码缺陷。静态代码分析工具能够在代......
  • 基于simulink的信道化接收机建模与仿真
    目录1.发送模块设计2.接收模块的设计3.仿真测试4.基于matlab的误码率仿真1.发送模块设计16QAM的基本结构,首先我来设计QAM发送段的SIMULINK仿真模块。  基本工......