• ANDROID中ACTIVITY间的数据传递


    效果:有两个Activity分别为A和B,从A中采用Bundle封装数据向B中传递数据,然后使用startActivityForResult在B中修改后回传数据。

    第一个Activity的layout如main.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="fill_parent"
     5     android:layout_height="fill_parent">
     6     <TextView  
     7         android:id="@+id/text"
     8         android:layout_width="fill_parent" 
     9         android:layout_height="wrap_content" 
    10         android:text="Welcome to my blog:"/>
    11     <Button 
    12         android:id="@+id/edit_btn"
    13         android:layout_width="fill_parent" 
    14         android:layout_height="wrap_content"
    15         android:text="编辑"/>
    16 </LinearLayout>

    效果如下:

    刚开始只是一个TextView显示Welcome to my blog:现在需要的是在点击编辑按钮时将TextView里的数据传递给第二个Activity编辑,然后回传编辑后的数据。

    在第一个Activity中,采用Bundle封装并传递数据,从TextView里获取文本用getText()方法,核心代码如下

    1         text = (TextView)findViewById(R.id.text);
     2         editBtn = (Button)findViewById(R.id.edit_btn);
     3         editBtn.setOnClickListener(new OnClickListener(){
     4             @Override
     5             public void onClick(View v) {
     6                 // TODO Auto-generated method stub
     7                 Bundle bundle = new Bundle();
     8                 bundle.putString("text_view", text.getText().toString());
     9                 Intent intent = new Intent(DataTransActivity.this, SecondActivity.class);
    10                 intent.putExtras(bundle);
    11                 startActivityForResult(intent, 0);
    12             }
    13         });

    Android中startActivityForResul(Intent intent, int requestCode)的主要作用就是它可以回传数据,这里需注意的是requestCode要大于等于0,用于回传时识别用的。

    好了,数据是发出去了,那怎样在第二个Activity接收呢?先开布局文件:

    1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="fill_parent"
     5     android:layout_height="fill_parent"
     6     >
     7     <EditText  
     8         android:id="@+id/edittext"
     9         android:layout_width="fill_parent" 
    10         android:layout_height="300dp" />
    11     <LinearLayout 
    12         android:orientation="horizontal"
    13         android:layout_width="fill_parent"
    14         android:layout_height="wrap_content">
    15         <Button 
    16             android:id="@+id/submit"
    17             android:layout_width="0dp" 
    18             android:layout_height="wrap_content"
    19             android:layout_weight="1.0"
    20             android:text="确定"/>
    21         <Button 
    22             android:id="@+id/cancel"
    23             android:layout_width="0dp" 
    24             android:layout_height="wrap_content"
    25             android:layout_weight="1.0"
    26             android:text="取消"/>
    27     </LinearLayout>
    28 </LinearLayout>

    效果图如下,图中文字http://www.cnblogs.com/sense7/是我另外加进去的,从主Activity传递过来的数据是Welcome to my blog:确定按钮会把修改过的数据回传,取消按钮则会返回主Activity然后提示未作任何修改。

    首先接收主Activity穿过来的数据:

    1         Bundle bundle = getIntent().getExtras();
    2         editText.setText(bundle.getString("text_view"));

    接下来确定按钮将修改过的数据封装回传:

    1         submit.setOnClickListener(new OnClickListener(){
     2             @Override
     3             public void onClick(View v) {
     4                 // TODO Auto-generated method stub
     5                 // 实例化 Bundle,设置需要传递的参数
     6                 Bundle bundle = new Bundle(); 
     7                 bundle.putString("edit_text", editText.getText().toString()); 
     8                 SecondActivity.this.setResult(RESULT_OK, SecondActivity.this.getIntent().putExtras(bundle));
     9                 SecondActivity.this.finish();
    10             }
    11         });

    点击取消按钮

    1         cancel.setOnClickListener(new OnClickListener(){
    2             @Override
    3             public void onClick(View v) {
    4                 // TODO Auto-generated method stub
    5                 SecondActivity.this.setResult(RESULT_CANCELED);
    6                 SecondActivity.this.finish();
    7             }
    8         });

    其中setResut(int resultCode, Intent intent)将结果回传,resultCode用于区分不同的返回结果,RESULT_OK和RESULT_CANCELED是自带的参数,方便使用,在下面还会提一下,而intent中可以封装修改后的数据传回给主Activity。还有一点就是,最后要调用当前Activity的finish()方法。

    下面在主Activity中重写onActivityResult(int requestCode, int resultCode, Intent data)方法,requestCode和startActivityForResul(Intent intent, int requestCode)中requestCode的对应,resultCode则是上面穿过来的值,data是回传的值。重写方法的代码为:

    1     @Override
     2     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     3         // TODO Auto-generated method stub
     4         super.onActivityResult(requestCode, resultCode, data);
     5         if (resultCode == RESULT_OK) { 
     6             Bundle bundle = data.getExtras(); 
     7             text.setText(bundle.getString("edit_text").toString());
     8         }else if (resultCode == RESULT_CANCELED) { 
     9             Toast.makeText(this, "未作任何修改", Toast.LENGTH_SHORT).show(); 
    10         }
    11     }

    修改文字点击确定返回后的效果图如下

    点击取消效果如下

     

     

  • 相关阅读:
    layui动态修改select的选中项
    ES6数组新增方法
    A Realtime BigData Dashboad
    Spark Streaming
    使用Converter实现控件的动态显隐
    Mysql 使用mysqldump进行备份与还原
    大型Java进阶专题(六) 设计模式之代理模式
    HTML开发之--marquee标签
    iOS开发之--instancetype和id
    请求处理常见tag语法
  • 原文地址:https://www.cnblogs.com/xiaoran1129/p/2576157.html
Copyright © 2020-2023  润新知