.Net程式員玩轉Android開發---(11)頁面跳轉,.netandroid

來源:互聯網
上載者:User

.Net程式員玩轉Android開發---(11)頁面跳轉,.netandroid

       在任何程式開發中,都會遇到頁面之間跳轉的情況,Android開發也不例外.這一節,我們來認識下Android項目中怎樣進行頁面跳轉.頁面跳轉分為有參數和無參數頁面跳轉,已經接受另一個頁面的返回值等。Android中頁面跳轉常用到的是Intent ,但是Intent不僅用做頁面跳轉,還可以做其他事情,例如撥打到電話,傳送簡訊,調用其他程式等。這節我們主要認識下怎樣通過Intent進行頁面跳轉.

          1.頁面跳轉

                          我們首先簡單認識下Intent,Intent有有幾個重載建構函式。我們使用其中一個建構函式進行頁面跳轉。

                           Intent intent = new Intent(MainActivity.this,SecondActivity.class);  有兩個參數,第一個參數代表當前頁面對象,第二個參數代表要跳轉到的目標對象。

                         建立完Intent後,我們使用startActivity進行啟動。、

                         下面我們看下這個效果

                         啟動頁面

                         

                    

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="74dp"        android:text="跳轉到下一個頁面" /></RelativeLayout>


 

package com.example.hellotest;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn=(Button)findViewById(R.id.button1);        btn.setOnClickListener(new OnClickListener() //綁定註冊按鈕單擊事件                  {              @Override              public void onClick(View arg0) {                       // 按鈕跳轉                         Intent intent = new Intent(MainActivity.this,SecondActivity.class);                                  startActivity(intent);                }                                          });      }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    }


                        目標頁面

                         

<?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:id="@+id/textView1"        android:layout_width="219dp"        android:layout_height="wrap_content"        android:layout_weight="0.19"        android:text="第二個頁面"         /></LinearLayout>


 

          2.帶參數頁面跳轉

                             這個樣本,我們來看下頁面跳轉並傳值,首先A頁面跳轉到B頁面,並傳遞值,然後B頁面返回A頁面,同時向A頁面傳遞值。如下:

                     

                      發送內容到B頁面

                    

                   點擊返回到A頁面,並把頁面輸入內容傳遞到A頁面

                  

                 A頁面配置和代碼

                 

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">   <LinearLayout android:layout_width="fill_parent"  android:layout_weight="0.9" android:layout_height="fill_parent">    </LinearLayout>   <LinearLayout   android:layout_width="fill_parent"  android:layout_weight="0.1"  android:orientation="vertical"  android:layout_height="fill_parent">          <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">            <TextView android:textSize="8pt"    android:text="發送內容:"    android:id="@+id/tvSend"    android:layout_weight="0.7"    android:layout_width="fill_parent"    android:layout_height="wrap_content">                    </TextView>                <EditText     android:layout_weight="0.3"     android:layout_width="fill_parent"     android:text=""     android:id="@+id/etmsg"     android:layout_height="wrap_content">                    </EditText>            </LinearLayout>          <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">     <Button android:text="發送"        android:textSize="9pt"      android:layout_width="fill_parent"         android:layout_height="wrap_content"      android:id="@+id/btnSend"      >   </Button>      </LinearLayout>  <LinearLayout android:layout_width="fill_parent"  android:layout_weight="0.9" android:layout_height="fill_parent">           <TextView android:textSize="8pt"    android:text="接受返回內容:"    android:id="@+id/tvreturn"    android:layout_weight="0.7"    android:layout_width="fill_parent"    android:layout_height="wrap_content">                    </TextView>    </LinearLayout>   </LinearLayout>    </LinearLayout>


 

package com.example.hellotest;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class OrginLayOut  extends Activity {private Button btn;//發送按鈕private EditText edtiText;//發送內容private TextView tvReturn;//接受子頁面返回顯示的內容  protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.orginlayout);                btn=(Button)findViewById(R.id.btnSend);//擷取button對象        edtiText=(EditText)findViewById(R.id.etmsg);//擷取文字框對象        tvReturn=(TextView)findViewById(R.id.tvreturn);                btn.setOnClickListener(new OnClickListener() //綁定註冊按鈕單擊事件                  {              @Override              public void onClick(View arg0) {                       // 按鈕跳轉                    Intent intent = new Intent();                   intent.setClass(OrginLayOut.this, TargetActivity.class);             Bundle bundle = new Bundle();                 bundle.putString("msg",edtiText.getText().toString());//傳值                 intent.putExtras(bundle);                // startActivity(intent);                 startActivityForResult(intent, 0);              }                                          });      }  //接受頁面的傳回值  @Override//requestCode請求標識   //resultCode 返回標識    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if(requestCode == 0) {            if(resultCode == Activity.RESULT_OK) {               String content=data.getStringExtra("returnmsg");            tvReturn.setText("接受返回內容:"+content);            }        }  }}


            B頁面配置和代碼

            

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">   <LinearLayout android:layout_width="fill_parent"  android:layout_weight="0.9" android:layout_height="fill_parent">    </LinearLayout>   <LinearLayout   android:layout_width="fill_parent"  android:layout_weight="0.1"  android:orientation="vertical"  android:layout_height="fill_parent">     <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">              <TextView android:textSize="8pt"    android:text="接受內容:"    android:id="@+id/tvreceivemsg"    android:layout_weight="0.7"    android:layout_width="fill_parent"    android:layout_height="wrap_content">    </TextView>        </LinearLayout>      <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">            <TextView android:textSize="8pt"    android:text="返回內容:"    android:layout_weight="0.7"    android:layout_width="fill_parent"    android:layout_height="wrap_content">                    </TextView>                <EditText     android:layout_weight="0.3"     android:layout_width="fill_parent"     android:text=""     android:id="@+id/etreturnmsg"     android:layout_height="wrap_content">                   </EditText>            </LinearLayout>          <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">     <Button android:text="返回"        android:textSize="9pt"       android:layout_width="fill_parent"          android:layout_height="wrap_content"       android:id="@+id/btnreturn"       >   </Button>      </LinearLayout>   </LinearLayout>    </LinearLayout>


 

package com.example.hellotest;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.EditText;import android.widget.TextView;public class TargetActivity extends Activity { private TextView tv; private Button btn; private EditText returnText; protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.targetlayout);        tv=(TextView)findViewById(R.id.tvreceivemsg);        btn=(Button)findViewById(R.id.btnreturn);//擷取button對象        returnText=(EditText)findViewById(R.id.etreturnmsg);//擷取文字框對象                Bundle bunde = this.getIntent().getExtras();        String strs="接受內容:"+bunde.getString("msg").toString();        tv.setText(strs);                btn.setOnClickListener(new OnClickListener() //綁定註冊按鈕單擊事件               {           @Override           public void onClick(View arg0) {                    // 按鈕跳轉                 Intent  data= new Intent();               data.putExtra("returnmsg",returnText.getText().toString());              setResult(Activity.RESULT_OK,data);              finish();           }                                 });      }}

                             這裡有幾個方法我們簡單說明下:
                             startActivityForResult 如果父頁面想接受子頁面的返回值,使用這個方法啟動子頁面,方法第一個參數代表啟動的子頁面資訊,第二個參數代表請求碼,

           是整數類型。 請求碼requestCode的意思是如果一個子頁面有多個父頁面可以啟動,通過請求碼可以判斷來自哪個父頁面。

                           onActivityResult(int requestCode, int resultCode, Intent data) 用來接收父頁面返回的內容的方法,第一個參數是請求碼,第二個參數是返回結果標識resultCode, resultCode主要用來判斷哪個子頁面返回。 第三個參數就是返回資料

                           setResult(Activity.RESULT_OK,data); 這個方法主要用於子頁面,第一個參數是返回標識,第二個參數是返回資料

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

                  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.