利用Intent在不同的activity之间传递数据

public   class   MainActivity   extends   Activity {

      
       private   EditText   ed_name ;
       private   ProgressBar    pb ;
       private   TextView     tv ;
      
       @Override
       protected   void   onCreate(Bundle savedInstanceState) {
               super .onCreate(savedInstanceState);
            setContentView(R.layout.   activity_main );
            
               //拿到控件
               ed_name =(EditText) findViewById(R.id.   et_name );
               pb =(ProgressBar) findViewById(R.id.   pb );
               tv =(TextView) findViewById(R.id.   tv );
            
   }
      
       /**
       * Intent传递数据
       */
       public   void   click(View view){
            
               tv .setVisibility(View.   VISIBLE ) ;    //方法调用时tv可见   
               final   String name =   ed_name .getText().toString().trim();     //得到姓名
            
           new   Thread(){
                     public   void   run() {
                           byte [] results = name.getBytes();
                           int   sum=0;
                           pb .setMax(results.   length );      //设置进度条的最大值
                           for (   int   i=0;i<results.   length ;i++){
                                   int   number=(   int )results[i];
                                 sum=sum+number;
                                   try   {
                                    Thread. sleep(300);
                              }   catch   (InterruptedException e) {
                                    
                                    e.printStackTrace();
                              }
                                
                                   pb .setProgress(i);      //设置进度条变化
                        }
                        
                           int   rp=Math.abs (sum%100);
                        
                           //创建意图开启一个新的界面,并传递数据
                        Intent intent=   new   Intent(getApplicationContext(),RPActivity. class   );
                        intent.putExtra(   "name" ,   "HELLO" );
                        intent.putExtra(   "rp" , 123);
                        
                        startActivity(intent);
                        
                  };
                  
                  
                  
            }.start();
            
            
            
            
      }
      

--------------------------------------------------------------------------------------------------



public   class   RPActivity   extends   Activity {
      
       private   TextView    tv_info ;
       private   TextView    result ;
      
      @Override
      protected   void   onCreate(Bundle savedInstanceState) {
       super .onCreate(savedInstanceState);
      setContentView(R.layout.   rp );
      
       //拿到控件
       tv_info =(TextView) findViewById(R.id.   tv_info );
       result =(TextView) findViewById(R.id.   tv_result );
      
          //创建意图
      Intent intent= new   Intent();
      String name=intent.getStringExtra(   "name" );     //接收传递过来的参数
       int   rp=intent.getIntExtra(   "rp" , 0);
      
       tv_info .setText(name+   "的人品是: " );
      
       if (rp<50){
               tv_info .setTextColor(Color.   GREEN );
               tv_info .setText(   "计算的人品是:"   +rp);
      } else {
               tv_info .setTextColor(Color.   BLUE );
               tv_info .setText(   "计算的人品是:"   +rp);
            
      }
      
      
      
      
    }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值