Android知识Android开发Android技术知识

Activity间传值方式

2016-08-27  本文已影响4435人  哇楼主

方式二:
页面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); 

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();
}   
}
上一篇下一篇

猜你喜欢

热点阅读