23.Intent传值,Bundle包传值

Posted gzyx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了23.Intent传值,Bundle包传值相关的知识,希望对你有一定的参考价值。

package com.example.p85;

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.RadioButton;


public class MainActivity extends Activity {

    //选择的代表版本的radiobutton
    private RadioButton rb_1,rb_2,rb_3; 
    private Button bt_submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lin);
    
        //找到关心的控件
        bt_submit = (Button)findViewById(R.id.bt_submit);
        rb_1 = (RadioButton)findViewById(R.id.rb1);
        rb_2 = (RadioButton)findViewById(R.id.rb2);
        rb_3 = (RadioButton)findViewById(R.id.rb3);
      
        bt_submit.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                
    //?????????这里的占位代表了什么
                Intent ii = new Intent(MainActivity.this,second.class);
                
                
                Bundle myBundle = new Bundle();
                
                
                //判断选中哪一个,判断完成使用intent传输数据到第二个
                if(rb_1.isChecked()){
                //ii.putExtra("data", (String)rb_1.getText().toString());
                myBundle.putString("data", (String)rb_1.getText().toString());
                
                }else if (rb_2.isChecked()) {
                //ii.putExtra("data", (String)rb_2.getText().toString());    
                myBundle.putString("data", (String)rb_2.getText().toString());
                }else if (rb_3.isChecked()) {
                //ii.putExtra("data", (String)rb_3.getText().toString());    
                myBundle.putString("data", (String)rb_3.getText().toString());
                }else {
                //ii.putExtra("data", "null");    
                }
                
                ii.putExtras(myBundle);
                
                startActivity(ii);
            }
        });
    }
    
    


    
}

 

 

package com.example.p85;

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

public class second extends Activity{

    private TextView tv;
    private Button bt;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lin2);
        
        bt = (Button)findViewById(R.id.bu_back);
        tv = (TextView)findViewById(R.id.tv_receive);
        
        //接受在第一个activity中发送来了意图
        Intent my = this.getIntent();
        
        //获得意图中带有的bundle包
        Bundle mmBundle = my.getExtras();
        
        //使用bundle包拿到的数据
        String ss1 = mmBundle.getString("data");
        
        //使用意图拿到的数据
        String ss = my.getStringExtra("data");
        
        if("".equals(ss1)){
            tv.setText("没有内容");
        }else {
            tv.setText(ss1+"被选中了");    
        }
        
        
        
        bt.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                
                //跳转回去可以通过finish掉当前的activity
                finish();
                
            }
        });
    }
    
}

以上是关于23.Intent传值,Bundle包传值的主要内容,如果未能解决你的问题,请参考以下文章

DialogFragment的传值问题

Android 完美解决bundle实现页面跳转并保留之前数据+传值

android activity中的变量怎么传值给fragment

跨域访问方法介绍--使用片段识别符传值

block 从B界面向A界面传值

React教程:父子组件传值(组件通信)