Android中利用Intent和Bundle传值总结
来源:互联网 发布:生死狙击刷矩阵要多久 编辑:程序博客网 时间:2024/05/29 17:39
一、startActivity() + Intent传值
在MainActivity.java的布局文件中添加一个按钮:
<Button android:id="@+id/id_intent_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Intent 传值" />在MainActivity.java的onCreate()方法中:
Button btn = (Button) findViewById(R.id.id_intent_btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this,SecondActivity.class); intent.putExtra("gender",1); intent.putExtra("name","liying"); startActivity(intent); } });
至此就实现了把name和gender发送到SecondActivity.接下来就是在SecondActivity.class中取出值。
在SecondActivity.java 的onCreate()方法中:
public class SecondActivity extends AppCompatActivity { private static final String TAG = "SecondActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); String gender_str = ""; String name = getIntent().getStringExtra("name"); int gender = getIntent().getIntExtra("gender",0); if (0==gender){ gender_str = "男"; } if (1==gender){ gender_str = "女"; } Log.e(TAG, "onCreate: 从MainActivity中传过来的name="+name+" , gender="+gender_str ); }}
这样我们就通过startActivity() + intent的方式实现了传值。进入到Intent的源代码可以看到,intent传值可以传递各种不同的数据类型。
Intent putExtra(String name, int[] value) Intent putExtra(String name, float value) Intent putExtra(String name, byte[] value) Intent putExtra(String name, long[] value) Intent putExtra(String name, float[] value) Intent putExtra(String name, long value) Intent putExtra(String name, String[] value) Intent putExtra(String name, boolean value) Intent putExtra(String name, boolean[] value) Intent putExtra(String name, short value) Intent putExtra(String name, double value) Intent putExtra(String name, short[] value) Intent putExtra(String name, String value) Intent putExtra(String name, byte value) Intent putExtra(String name, char[] value) Intent putExtra(String name, CharSequence[] value)
二、Bundle + startActivity() 传值
首先要知道什么是Bundle?
Bundle可以理解为键值对的集合。利用Bundle实现两个Activity的传值步骤如下:
首先在MainActivity.java的布局文件中添加一个按钮:
<Button android:id="@+id/id_bundle_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Bundle 传值" />然后在MainActivity.java的onCreate()中:
Button btn_bundle = (Button) findViewById(R.id.id_bundle_btn); btn_bundle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //创建Bundle对象 Bundle mBundle = new Bundle(); //往bundle里面put值 mBundle.putString("name","周杰伦"); mBundle.putString("gender","男"); mBundle.putBoolean("isStar",true); mBundle.putInt("height",180); //创建Intent Intent intent = new Intent(MainActivity.this,SecondActivity.class); //注意是putExtras() intent.putExtras(mBundle); startActivity(intent); } });
至此实现了利用Bundle把值从MainActivity传递到SecondActivity.接下来是如何从SecondActivity.java中取值。
同样在SecondActivity.java的onCreate()方法中:
Bundle mBundle = getIntent().getExtras(); String name2 = mBundle.getString("name"); String gender2 = mBundle.getString("gender"); boolean isStar = mBundle.getBoolean("isStar"); int height = mBundle.getInt("height"); Log.e(TAG, "onCreate: 姓名="+name2+" , 性别=" +gender2 + " , 是否为Star="+ isStar + " , 身高="+height);打印出的log信息为:
这样我们就成功利用Bundle+startActivity()的方式实现了传值。
三、Intent 和 Bundle的区别
在方式一中,以传递字符串为例,查看Intent的源码,方法为:
public Intent putExtra(String name, String value) { if (mExtras == null) { mExtras = new Bundle(); } mExtras.putString(name, value); return this; }我们可以看到,putExtra(...)方法实质上还是利用Bundle来传值。如何选择用哪种方式可以根据具体的情况,如果只需要传递一个值可以选择用Intent的方式,如果需要传递多个值到第二个页面,可以选择Bundle的方式。如果需要把值从第一个页面传递到第二个页面,再添加几个值到第三个页面,选择Bundle传值就会方便很多。
四、startActivityForResult() + Intent / Bundle 的方式传值
startActivity() 和 startActivityForResult() 区别是:
startActivity() 可以实现A页面跳转到B页面,而B页面回到A页面需要再次调用startActivity()方法。
startActivityForResutl()方法则是可以实现A页面打开B页面,当B执行了finish()方法后,程序会自动跳转会A页面。并且跳转回A页面的时候可以传值,即B回传的数据。当然A跳转到B的时候也可以进行传值。
首先在MainActivity.java的布局文件中添加一个Button:
<Button android:id="@+id/id_forResult_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="startActivityForResult 传值" />然后在MainActivity.java的onCreate()方法中:
Button forResult_btn = (Button) findViewById(R.id.id_forResult_btn); forResult_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("msg1", "你好我是A"); startActivityForResult(intent, 666);//666 是requestCode 可以理解为一个标识,要>=0 } }至此我们就利用startActivityForResult() + Intent 实现了打开SecondActivity页面并且传递过去一个值。
然后在SecondActivity.java中
首先在SecondActivity.java的布局文件中,添加了一个TextView,用来显示从MainActivity传递过来的值。
<?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" android:orientation="vertical" tools:context="com.ly.lypopupwindowdemo.SecondActivity"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" android:textColor="#333" android:textSize="18sp" /></LinearLayout>然后在SecondActivity.java的onCreate()中:
TextView text = (TextView) findViewById(R.id.text); //接受MainActivity传过来的值,并显示再TextView中,然后告诉MainActivity我已经收到了消息。 String str = getIntent().getStringExtra("msg1"); Log.e(TAG, "onCreate: str1=="+str ); text.setText(str); //创建一个Bundle对象,用来把数据传递到MainActivity中 Bundle bundle = new Bundle(); bundle.putString("msg2","收到了消息,你好,我是B"); bundle.putString("phone","15632031701"); final Intent intent = new Intent(); intent.putExtras(bundle); new Handler().postDelayed(new Runnable() { @Override public void run() { setResult(RESULT_OK,intent);//setResult()方法必须在finish()之前 finish();//此处一定要调用finish()方法 } },5000);
为了能够看清,SecondActivity确实打开了,所以开启了一个线程,延迟5秒再关闭当前页面。
如何在MainActivity.java中接收到回传过来的数据呢?需要我们在MainActivity.java中重写onActivityResult(.....)方法。
在MainActivity.java中:
private static final String TAG = "MainActivity"; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { return; } switch (requestCode) { case 666: Bundle bundle = data.getExtras(); String str2 = bundle.getString("msg2"); String phone = bundle.getString("phone"); Log.e(TAG, "onActivityResult: 收到SecondActivity的值==" + str2 + ", phone =" + phone); break; default: break; } super.onActivityResult(requestCode, resultCode, data); }打印log信息:
总结一下涉及到的函数有三个:
startActivityForResult(Intent intent, Int requestCode)
setResut(int resultCode, Intent intent)
onActivityResult(int requestCode, int resultCode, Intent intent)
其中需要注意,A页面通过startActivityForResult()方法打开B页面,B调用setResult()并不会马上把数据回传给A,只有当B调用了finish()方法,才会把值回传给A的onActivityResult() 中去处理。
五、自定义方法传值
直接上栗子......
首先创建一个MyWebViewActivity.java
布局文件:
<?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" android:orientation="vertical" tools:context="com.ly.lypopupwindowdemo.MyWebViewActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" /> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:text="标题" android:textSize="18sp" /> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="3dp" android:background="#3F51B5" /> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>MyWebViewActivity.java:
public class MyWebViewActivity extends AppCompatActivity { private Button back; private TextView title; private WebView webView; /** * load一个网页 * * @param context * @param title * @param url */ public static void startWebView(Context context, String title, String url) { Intent intent = new Intent(context, MyWebViewActivity.class); intent.putExtra("title", title); intent.putExtra("linkItem", url); context.startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_web_view); back = (Button) findViewById(R.id.back); title = (TextView) findViewById(R.id.title); webView = (WebView) findViewById(R.id.webview); String strItem = getIntent().getStringExtra("linkItem"); String title_str = getIntent().getStringExtra("title"); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); WebSettings ws = webView.getSettings(); ws.setJavaScriptEnabled(true); webView.loadUrl(strItem); title.setText(title_str); webView.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { webView.loadUrl(url); return true; } }); }}
在MainActivity.java中:
同样现在MainActivity.java的布局文件中添加一个按钮
<Button android:id="@+id/start_webview_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="打开一个网页" />
然后java代码是:
Button start_webview = (Button) findViewById(R.id.start_webview_btn); start_webview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyWebViewActivity.startWebView(MainActivity.this,"百度","http://www.baidu.com"); } });
这样就把title 和 网址传递到了MyWebViewActivity中了~~~~~
以上.......
等等,我看大牛的博客都要有个gif运行图,我也要跟上大牛的脚步,推荐一个录屏的工具,GifCam,具体使用参看:
http://blog.csdn.net/u012400885/article/details/53999569
- Android中利用Intent和Bundle传值总结
- android中intent bundle
- Android中Intent传值和Bundle传值两者的不同
- Android Intent传值和Bundle传值的异同
- Android用Intent和Bundle传list
- Android用Intent和Bundle传list
- Android中Context,Intent,Bundle
- Android中intent传递对象和Bundle的用法
- Android中intent传递对象和Bundle的用法
- Android中intent传递对象和Bundle的用法
- Android中intent传递对象和Bundle的用法
- Android中intent传递对象和Bundle的用法
- Android中intent传递对象和Bundle的用法
- android中Intent传值与Bundle传值的区别详解
- android中Intent传值与Bundle传值的区别详解
- Android中Intent与Bundle 在传值时有什么不同
- Android中Intent与Bundle 在传值时有什么不同
- Android中Intent与Bundle 在传值时有什么不同
- Android EditText达到SearchView的效果
- FFMPEG学习【libavcodec】:发送/接收编码和解码API概述
- ubuntu下sublimetext3输入中文的方法
- 【剑指offer】重建二叉树
- 技术-Homebrew
- Android中利用Intent和Bundle传值总结
- Windows中部分常用快捷键
- Java泛型浅谈(2)
- charles破解
- python学习笔记-turtle类库实例
- COM组件学习
- Swift 3.0 控制流
- IDEA 打jar,提交spark集群运行
- onTouchEven事件分发机制