Intent实现页面跳转
生活随笔
收集整理的這篇文章主要介紹了
Intent实现页面跳转
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上面2是有返回結果,也就是頁面A與B有數據傳遞。
?
1.無返回結果的頁面跳轉方式:
FirstActivity.java (主Activity)
package com.example.test2;import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener;public class FirstActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.first_activity);Button btn = (Button) findViewById(R.id.btn1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub/*** 第一個參數:上下文(由于匿名內部類不能訪問局部變量,所以需要帶類名)* 第二種方式是:定義一個全局變量Context,初始化之后進行訪問* 第二個參數:目標文件*/Intent intent = new Intent(FirstActivity.this,SecondActivity.class);FirstActivity.this.startActivity(intent);}});}}?
?first_activity.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" ><Button android:id="@+id/btn1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="第一種跳轉方式"/><Button android:id="@+id/btn2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="第二種跳轉方式"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="將第二個頁面的結果寫到這里"/></LinearLayout>?
?SecondActivity.java (第二個頁面)
package com.example.test2;import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener;public class SecondActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second_activity);}}?
second_activity.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" ><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="第二個頁面的按鈕"/></LinearLayout>?
AndroidManifest.xml 清單文件注冊Activity
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.test2"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="19" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.test2.FirstActivity"android:label="第一個界面" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name="com.example.test2.SecondActivity" android:label="第二個界面"></activity></application></manifest>?
效果:
?
?
?
?
?2.有返回結果的跳轉方式
一個請求碼,一個結果碼確定是某個頁面的回傳結果。
?FirstActivity
創建Intent,startActivityForResult是打開第二個頁面(帶著一個請求參數)
onActivityResult:處理回傳的數據(根據請求碼與回傳碼確定一個頁面)
?
package com.example.test2;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.TextView;public class FirstActivity extends Activity {private TextView textView1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.first_activity);Button btn = (Button) findViewById(R.id.btn1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub/*** 第一個參數:上下文(由于匿名內部類不能訪問局部變量,所以需要帶類名)* 第二種方式是:定義一個全局變量Context,初始化之后進行訪問* 第二個參數:目標文件*/Intent intent = new Intent(FirstActivity.this,SecondActivity.class);FirstActivity.this.startActivity(intent);}});textView1 = (TextView) findViewById(R.id.textView1);Button btn2 = (Button) findViewById(R.id.btn2);btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub/*** 第一個參數:上下文(由于匿名內部類不能訪問局部變量,所以需要帶類名)* 第二種方式是:定義一個全局變量Context,初始化之后進行訪問* 第二個參數:目標文件*/Intent intent = new Intent(FirstActivity.this,SecondActivity.class);/*** intent:目標文件* 1:請求碼*/startActivityForResult(intent, 1);}});}/** 處理回傳的數據:* requestCode:請求碼* resultCode:結果碼* data:數據(格式像map)* */@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);//如果請求碼是1,結果碼是2證明是點擊第二個按鈕時的頁面回傳的結果if(requestCode==1&&resultCode==2){textView1.setText(data.getStringExtra("data"));}}}?
?
?first_activity.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" ><Button android:id="@+id/btn1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="第一種跳轉方式"/><Button android:id="@+id/btn2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="第二種跳轉方式"/><TextView android:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="將第二個頁面的結果寫到這里"/></LinearLayout>?
?SecondActivity.java
點擊按鈕的時候,回傳數據并且結束自己的頁面
package com.example.test2;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;public class SecondActivity extends Activity {private Button btn3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second_activity);btn3 = (Button) findViewById(R.id.sebtn);/*** 回傳到第一個頁面實際是一個intent對象*/btn3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//因為不做頁面跳轉所以不設置參數Intent data = new Intent();data.putExtra("data", "你好");/*** 回傳數據* 第一個參數是回傳碼,第二個是數據*/setResult(2, data);//結束當前頁面,回傳到第一個頁面 finish();}});}}?
?
?second_activity.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" ><Button android:id="@+id/sebtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="第二個頁面的按鈕"/></LinearLayout>?
?
?
效果:
?
?
?
?
?
轉載于:https://www.cnblogs.com/qlqwjy/p/7515984.html
總結
以上是生活随笔為你收集整理的Intent实现页面跳转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jmeter--正则提取json串中一个
- 下一篇: dbms_stats包更新、导出、导入、