码迷,mamicode.com
首页 > 其他好文 > 详细

23.Intent传值,Bundle包传值

时间:2019-12-24 10:07:20      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:string   star   传输数据   gen   cte   method   als   cti   div   

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包传值

标签:string   star   传输数据   gen   cte   method   als   cti   div   

原文地址:https://www.cnblogs.com/gzyx/p/12089252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!