Android Intent实现页面跳转

时间:2022-05-09 22:02:10
 
Intent可以来协助完成Android各个组件之间的通信
 
1:startActivity(intent);     //直接启动
               /*
               * 通过监听点击事件跳转套第二个activity
               */
               button=(Button) findViewById(R.id. button1);
               button.setOnClickListener( new OnClickListener() {
                      @Override
                      public void onClick(View v) {
                           Intent intent= new Intent(MainActivity.this ,myactivity.class);
                            //通过startactivity的方式实现
                           MainActivity. this.startActivity(intent);
                     }
              });
 
2:startActivityForResult(intent,requestCode);     //启动之后有返回值,需要关联onActivityResult(A页面使用),setResult(B页面使用)两个方法。
     onActivityResult(int requestCode,int resultCode,Intent data)    
     setResult(resultCode,data);
Android Intent实现页面跳转
button2=(Button) findViewById(R.id.button2);
               button2.setOnClickListener( new OnClickListener() {
                     
                      @Override
                      public void onClick(View v) {
                            // TODO Auto-generated method stub
                           Intent intent= new Intent(MainActivity.this ,myactivity.class);
                            /*
                            * 第一个参数是intent对象
                            * 第二个参数是请求的标识
                            */
                           startActivityForResult(intent, 1);
                     }
              });
 
 
 
      /**
        * 通过startActivityForResult跳转,接受返回数据的方法
        * requestCode:请求的标识
        * resultCode 第二个页面返回的标识
        * data 第二个页面回传的数据
        */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
               super.onActivityResult(requestCode, resultCode, data);
               if(requestCode==1&&resultCode==2){
                     String content=data.getStringExtra("data");
              }
       }