.Net程序员玩转Android开发---(11)页面跳转

   在任何程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中怎样进行页面跳转.页面跳转分为有参数和无参数页面跳转,已经接受另一个页面的返回值等。Android中页面跳转常用到的是Intent ,但是Intent不仅用做页面跳转,还可以做其他事情,例如拨打电话,发送短信,调用其他程序等。这节我们主要认识下怎样通过Intent进行页面跳转.

          1.页面跳转

                          我们首先简单认识下Intent,Intent有有几个重载构造函数。我们使用其中一个构造函数进行页面跳转。

                           Intent intent = new Intent(MainActivity.this,SecondActivity.class);  有两个参数,第一个参数代表当前页面对象,第二个参数代表要跳转到的目标对象。

                         创建完Intent后,我们使用startActivity进行启动。、

                         下面我们看下这个效果

                         启动页面

                          

                    

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <Button  
  12.         android:id="@+id/button1"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_centerHorizontal="true"  
  17.         android:layout_marginTop="74dp"  
  18.         android:text="跳转到下一个页面" />  
  19.   
  20. </RelativeLayout>  


 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.hellotest;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.       
  14.     private Button btn;  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         btn=(Button)findViewById(R.id.button1);  
  20.         btn.setOnClickListener(new OnClickListener() //绑定注册按钮单击事件     
  21.                {    
  22.             @Override    
  23.             public void onClick(View arg0) {    
  24.                      // 按钮跳转     
  25.                       Intent intent = new Intent(MainActivity.this,SecondActivity.class);    
  26.             
  27.                       startActivity(intent);    
  28.               }    
  29.                   
  30.                   
  31.         });    
  32.   
  33.     }  
  34.   
  35.   
  36.     @Override  
  37.     public boolean onCreateOptionsMenu(Menu menu) {  
  38.         // Inflate the menu; this adds items to the action bar if it is present.  
  39.         getMenuInflater().inflate(R.menu.main, menu);  
  40.         return true;  
  41.     }  
  42.       
  43. }  


                        目标页面

                         

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="219dp"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_weight="0.19"  
  12.         android:text="第二个页面"  
  13.          />  
  14.   
  15. </LinearLayout>  


 

          2.带参数页面跳转

                             这个示例,我们来看下页面跳转并传值,首先A页面跳转到B页面,并传递值,然后B页面返回A页面,同时向A页面传递值。效果图如下:

                      

                      发送内容到B页面

                     

                   点击返回到A页面,并把页面输入内容传递到A页面

                   

                 A页面布局和代码

                 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.    
  8.   <LinearLayout android:layout_width="fill_parent"  android:layout_weight="0.9" android:layout_height="fill_parent">  
  9.     </LinearLayout>  
  10.   
  11.   
  12.   
  13.    <LinearLayout   
  14.   android:layout_width="fill_parent"  
  15.   android:layout_weight="0.1"  
  16.   android:orientation="vertical"  
  17.   android:layout_height="fill_parent">  
  18.   
  19.       
  20.       
  21.   <LinearLayout  
  22.     android:layout_marginLeft="10px"  
  23.     android:layout_marginRight="10px"  
  24.     android:gravity="center"  
  25.     android:layout_width="fill_parent"  
  26.     android:orientation="horizontal"  
  27.     android:layout_height="wrap_content">  
  28.       
  29.         <TextView android:textSize="8pt"  
  30.     android:text="发送内容:"  
  31.     android:id="@+id/tvSend"  
  32.     android:layout_weight="0.7"  
  33.     android:layout_width="fill_parent"  
  34.     android:layout_height="wrap_content">  
  35.               
  36.         </TextView>  
  37.           
  38.         <EditText  
  39.      android:layout_weight="0.3"  
  40.      android:layout_width="fill_parent"  
  41.      android:text=""  
  42.      android:id="@+id/etmsg"  
  43.      android:layout_height="wrap_content">  
  44.               
  45.         </EditText>  
  46.         
  47.       </LinearLayout>  
  48.   
  49.   
  50.   
  51.       
  52.   
  53.       <LinearLayout   android:layout_marginLeft="10px"  android:layout_marginRight="10px" android:gravity="center"    android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">  
  54.      <Button android:text="发送"      
  55.     android:textSize="9pt"   
  56.      android:layout_width="fill_parent"     
  57.       android:layout_height="wrap_content"  
  58.       android:id="@+id/btnSend"  
  59.       >   </Button>  
  60.       </LinearLayout>  
  61.   
  62.   <LinearLayout android:layout_width="fill_parent"  android:layout_weight="0.9" android:layout_height="fill_parent">  
  63.      
  64.         <TextView android:textSize="8pt"  
  65.     android:text="接受返回内容:"  
  66.     android:id="@+id/tvreturn"  
  67.     android:layout_weight="0.7"  
  68.     android:layout_width="fill_parent"  
  69.     android:layout_height="wrap_content">  
  70.               
  71.         </TextView>  
  72.     
  73.   </LinearLayout>  
  74.    
  75.   
  76.   
  77.   
  78.   </LinearLayout>  
  79.       
  80. </LinearLayout>  


 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.hellotest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.SharedPreferences;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.TextView;  
  12.   
  13. public class OrginLayOut  extends Activity {  
  14.       
  15.     private Button btn;//发送按钮  
  16.     private EditText edtiText;//发送内容  
  17.     private TextView tvReturn;//接受子页面返回显示的内容  
  18.       protected void onCreate(Bundle savedInstanceState) {  
  19.             super.onCreate(savedInstanceState);  
  20.             setContentView(R.layout.orginlayout);  
  21.               
  22.             btn=(Button)findViewById(R.id.btnSend);//获取button对象  
  23.             edtiText=(EditText)findViewById(R.id.etmsg);//获取文本框对象  
  24.             tvReturn=(TextView)findViewById(R.id.tvreturn);  
  25.               
  26.             btn.setOnClickListener(new OnClickListener() //绑定注册按钮单击事件     
  27.                    {    
  28.                 @Override    
  29.                 public void onClick(View arg0) {    
  30.                          // 按钮跳转     
  31.                      Intent intent = new Intent();   
  32.                       intent.setClass(OrginLayOut.this, TargetActivity.class);  
  33.                      Bundle bundle = new Bundle();  
  34.                      bundle.putString("msg",edtiText.getText().toString());//传值  
  35.                      intent.putExtras(bundle);  
  36.                     // startActivity(intent);  
  37.                      startActivityForResult(intent, 0);  
  38.   
  39.                   }    
  40.                       
  41.                       
  42.             });    
  43.         }  
  44.   
  45.       //接受页面的返回值  
  46.       @Override//requestCode请求标识   //resultCode 返回标识  
  47.         protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  48.             if(requestCode == 0) {  
  49.                 if(resultCode == Activity.RESULT_OK) {  
  50.                    String content=data.getStringExtra("returnmsg");  
  51.                     tvReturn.setText("接受返回内容:"+content);  
  52.                 }  
  53.             }  
  54.       }  
  55. }  


            B页面布局和代码

            

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.    
  8.   <LinearLayout android:layout_width="fill_parent"  android:layout_weight="0.9" android:layout_height="fill_parent">  
  9.     </LinearLayout>  
  10.   
  11.   
  12.   
  13.    <LinearLayout   
  14.   android:layout_width="fill_parent"  
  15.   android:layout_weight="0.1"  
  16.   android:orientation="vertical"  
  17.   android:layout_height="fill_parent">  
  18.   
  19.      <LinearLayout     android:layout_marginLeft="10px"  
  20.     android:layout_marginRight="10px"  
  21.     android:gravity="center"  
  22.     android:layout_width="fill_parent"  
  23.     android:orientation="horizontal"  
  24.     android:layout_height="wrap_content">  
  25.       
  26.           <TextView android:textSize="8pt"  
  27.     android:text="接受内容:"  
  28.     android:id="@+id/tvreceivemsg"  
  29.     android:layout_weight="0.7"  
  30.     android:layout_width="fill_parent"  
  31.     android:layout_height="wrap_content">  
  32.     </TextView>  
  33.       
  34.     </LinearLayout>  
  35.       
  36.   <LinearLayout  
  37.     android:layout_marginLeft="10px"  
  38.     android:layout_marginRight="10px"  
  39.     android:gravity="center"  
  40.     android:layout_width="fill_parent"  
  41.     android:orientation="horizontal"  
  42.     android:layout_height="wrap_content">  
  43.       
  44.         <TextView android:textSize="8pt"  
  45.     android:text="返回内容:"  
  46.     android:layout_weight="0.7"  
  47.     android:layout_width="fill_parent"  
  48.     android:layout_height="wrap_content">  
  49.               
  50.         </TextView>  
  51.           
  52.         <EditText  
  53.      android:layout_weight="0.3"  
  54.      android:layout_width="fill_parent"  
  55.      android:text=""  
  56.      android:id="@+id/etreturnmsg"  
  57.      android:layout_height="wrap_content">  
  58.              
  59.         </EditText>  
  60.         
  61.       </LinearLayout>  
  62.   
  63.   
  64.   
  65.       
  66.   
  67.       <LinearLayout   android:layout_marginLeft="10px"  android:layout_marginRight="10px" android:gravity="center"    android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">  
  68.      <Button android:text="返回"     
  69.      android:textSize="9pt"   
  70.       android:layout_width="fill_parent"     
  71.        android:layout_height="wrap_content"  
  72.        android:id="@+id/btnreturn"  
  73.        >   </Button>  
  74.       </LinearLayout>  
  75.   
  76.    
  77.   
  78.   
  79.   
  80.   </LinearLayout>  
  81.       
  82. </LinearLayout>  


 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.hellotest;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.TextView;  
  11.   
  12. public class TargetActivity extends Activity {  
  13.   
  14.      private TextView tv;  
  15.      private Button btn;  
  16.      private EditText returnText;  
  17.      protected void onCreate(Bundle savedInstanceState) {  
  18.             super.onCreate(savedInstanceState);  
  19.             setContentView(R.layout.targetlayout);  
  20.             tv=(TextView)findViewById(R.id.tvreceivemsg);  
  21.             btn=(Button)findViewById(R.id.btnreturn);//获取button对象  
  22.             returnText=(EditText)findViewById(R.id.etreturnmsg);//获取文本框对象  
  23.               
  24.             Bundle bunde = this.getIntent().getExtras();  
  25.             String strs="接受内容:"+bunde.getString("msg").toString();  
  26.             tv.setText(strs);  
  27.               
  28.             btn.setOnClickListener(new OnClickListener() //绑定注册按钮单击事件     
  29.             {    
  30.          @Override    
  31.          public void onClick(View arg0) {    
  32.                   // 按钮跳转     
  33.               Intent  data= new Intent();   
  34.               data.putExtra("returnmsg",returnText.getText().toString());  
  35.               setResult(Activity.RESULT_OK,data);  
  36.               finish();  
  37.   
  38.            }    
  39.                
  40.                
  41.      });    
  42.         }  
  43. }  

                             这里有几个方法我们简单说明下:
                             startActivityForResult 如果父页面想接受子页面的返回值,使用这个方法启动子页面,方法第一个参数代表启动的子页面信息,第二个参数代表请求码,

           是整数类型。 请求码requestCode的意思是如果一个子页面有多个父页面可以启动,通过请求码可以判断来自哪个父页面。

                           onActivityResult(int requestCode, int resultCode, Intent data) 用来接收父页面返回的内容的方法,第一个参数是请求码,第二个参数是返回结果标识resultCode, resultCode主要用来判断哪个子页面返回。 第三个参数就是返回数据

                           setResult(Activity.RESULT_OK,data); 这个方法主要用于子页面,第一个参数是返回标识,第二个参数是返回数据

                   下载:http://download.csdn.net/detail/zx13525079024/8136253

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值