Activity间传值方式

  • 通过Intent意图传值
    方式一:
    页面A:Intent intent = new Intent(A.this,B.class);
    intent.putExtra(kay,value);
    startActivity(intent);

             页面B:Intent intent = getIntent();
             intent.getStringExtra(kay);
    

方式二:
页面A:Intent intent = new Intent(A.this,B.class);
Bundle bundle = new Bundle();
bundle.putString(key,value);
input.putExtras(bundle);
startActivity(intent);

           页面 B:Intent intent = getIntent();
           Bundle bundle = intent.getExtras();
            bundle.getString(key); 
  • 静态成员变量传值(不建议使用)
    方式:1.在页面A中声明一个公共的静态成员变量,在合适的位置存入数据
    2.直接访问页面A中的静态成员变量,读取相应的内容

  • 全局变量传值(Application)
    方式:1.创建Application的子类,并且在类中声明全局变量
    public class MyApplication extends Application {

             //应用的全局变量
            public Map<String,Object> data = new HashMap<String, Object>();
    
        }
    

2.在清单文件<Application />中通过android:name=”包名.类名”引入
3.在页面A合适位置存入数据
MyApplication application = (MyApplication) getApplication();//不能new MyApplication要用getApplication得到Application对象,向下转型得到MyApplication
application.data.put(“name”, “小翠”);
application.data.put(“age”, 21);
4.在页面B中取数据
MyApplication application = (MyApplication) getApplication();
String name = application.data.get(“name”).toString();
int age =Integer.parseInt(application.data.get(“age”).toString());

  • 回传数据

以选取用户头像为例:
页面A:startActivityForResult(intent,requestCode);
页面B:setResult(resultCode,Intent data);
页面A:重写onActivityResult(requestCode,resultCode);

页面A代码:

     public class MainActivity extends Activity {
                private ImageView iv;   
       @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = (ImageView) findViewById(R.id.iv);
  }  
  public void show(View v)
   {
    Intent intent = new Intent(MainActivity.this, InfoActivity.class);
    
    //带返回数据启动Activity
    startActivityForResult(intent, 110);
   }
   /**
  * 获取回传的数据
  * 
  * int requestCode  请求码
  * int resultCode   结果码
  * Intent data     意图(带着返回参数)
  */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==110 && resultCode == RESULT_OK)
    {
        //为图片控件设置内容
        
        int imageId = data.getIntExtra("imageId", R.drawable.ic_launcher);
        
        iv.setImageResource(imageId);
    }
  }    
  }

页面B:

public class InfoActivity extends Activity {

private RadioGroup rg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_info);
    
    rg = (RadioGroup) findViewById(R.id.rg);
}

//点击按钮后,将图片的ID 回传给MainActivity页面
public void submit(View v)
{
    //1, 得到当前选中的RadionButton的id
    int checkId = rg.getCheckedRadioButtonId();
    
    //图片的ID
    int imageId = R.drawable.ic_launcher;
    
    switch (checkId) {
    
    case R.id.rb1:
        
        imageId = R.drawable.img_31;
        
        break;

    case R.id.rb2:
        
        imageId = R.drawable.img_32;
        
        break;
        
    case R.id.rb3:
        
        imageId = R.drawable.img_33;
        
        break;
        
    case R.id.rb4:
        
        imageId = R.drawable.img_42;
        
        break;
        
    case R.id.rb5:
        
        imageId = R.drawable.img_43;
        
        break;
        
    case R.id.rb6:
        
        imageId = R.drawable.img_44;
        
        break;
    }
    
    
    /**
     * 通过startActivityForResult() 开启的界面, 需要有返回值,
     * 
     * 则必须要通过 setResult() 方法返回数据
     * 
     * resultCode  返回码
     * 
     * data        intent意图对象
     */
    Intent data = new Intent();
    data.putExtra("imageId", imageId);
    
    setResult(RESULT_OK, data);
    
    //关闭当前Activity页面
    finish();
}   
}
    原文作者:哇楼主
    原文地址: https://www.jianshu.com/p/f836432396f0
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞