Activity之间进行传值

首先在Activity之间进行跳转要在清单文件中进行注册例题是从MainActivity传值跳转到ResultActivity

具体例题在360云盘中http://yunpan.cn/cZNcQw3cf3Spb  访问密码 9d90

   < activity  android:name =  "com.example.activityTestRP.ResultActivity"  >
           
         </  activity >
标注: <  intent-filter >
                 <  action   android:name  = "android.intent.action.MAIN"  />

                 <  category   android:name  = "android.intent.category.LAUNCHER"  />
             </  intent-filter >
这两行是表示是否要创建快捷图标
   android:label  =  "@string/app_name"表示这个界面的标签
android:icon  =  "@drawable/ic_launcher"表示这个界面的图片
如果没有定义这两个的话,直接使用的是默认的标签和图片
xml文件
<  LinearLayout   xmlns:android  = "http://schemas.android.com/apk/res/android"
     xmlns:tools =  "http://schemas.android.com/tools"
     android:layout_width =  "match_parent"
     android:layout_height =  "match_parent"
     android:orientation =  "vertical"
      >
     < TextView
         android:layout_width  = "wrap_content"
         android:layout_height  = "wrap_content"
         android:layout_gravity  = "center_horizontal"
         android:textSize  = "28sp"
         android:textColor  = "#ff0000"
         android:text  = "人品测试"  />
     < EditText
         android:id  = "@+id/et_name"
         android:layout_width  = "match_parent"
         android:layout_height  = "wrap_content"
         android:hint  = "请输入姓名:"  />
     < Button     
         android:id  = "@+id/test"
         android:layout_width  = "match_parent"
         android:layout_height  = "wrap_content"
         android:text  = "开始测试"  />
</  LinearLayout >
Java文件
public   class  MainActivity  extends  Activity  implements  OnClickListener {
         private  EditText  et_name  ;
         @Override
         protected   void  onCreate(Bundle savedInstanceState) {
                super  .onCreate(savedInstanceState);
              setContentView(R.layout.  activity_main  );
       Button test = (Button) findViewById(R.id.  test  );
         et_name = (EditText) findViewById(R.id.  et_name  );
       test.setOnClickListener(  this  );
       }
         @Override
         public   void  onClick(View v) {
              String name=  et_name .getText().toString().trim();
                if  (TextUtils.isEmpty(name)){
                      //跳出一个警告
                       new  AlertDialog.Builder(  this ).setMessage(  "名字不能为空"  ).show();
              }
                else  {
                     Intent intent =  new Intent(this , ResultActivity. class);
                     //将值传到intent中
                     intent.putExtra( "name" ,name);
                     startActivity(intent);
              }
       }
}
第二个xml
<?  xml   version =  "1.0"   encoding  =  "utf-8" ?>
<  LinearLayout   xmlns:android  = "http://schemas.android.com/apk/res/android"
     android:layout_width =  "match_parent"
     android:layout_height =  "match_parent"
     android:orientation =  "vertical"   >
     < TextView   
     android:layout_width =  "wrap_content"
     android:layout_height =  "wrap_content"
     android:textSize =  "28sp"
     android:textColor =  "#ff0000"
     android:text =  "你的人品值"
     android:layout_gravity =  "center_horizontal"  />
    < TextView
     android:id =  "@+id/tv_result"
     android:layout_width =  "match_parent"
     android:layout_height =  "wrap_content"
     />
     <!--进度条-->
    < ProgressBar
        android:id  = "@+id/pb_testRP"
        style =  "?android:attr/progressBarStyleHorizontal"
        android:layout_width  = "fill_parent"
        android:layout_height  = "wrap_content"
        android:max  = "100"  />
</  LinearLayout >

Java文件
public   class  ResultActivity  extends  Activity {

         @Override
         protected   void  onCreate(Bundle savedInstanceState) {
                //  TODO  Auto-generated method stub
                super  .onCreate(savedInstanceState);
              setContentView(R.layout.  result  );
              TextView result =(TextView) findViewById(R.id.  tv_result  );
              
              Intent intent = getIntent();
              //获得前面传过来的name
              String name= intent. getStringExtra( "name");
              
              Random random =  new  Random();
                int  rp = random.nextInt(101);
              
              result.setText(name+  "您的人品值为:"  +rp);
              
              ProgressBar pb = (ProgressBar) findViewById(R.id.  pb_testRP  );
              //设置进度值
              pb.setProgress(rp);
       }
       
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值