实现页面跳转
一:无返回结果的页面跳转
1>新建一个Activity文件如MainActivity;重写onCreat方法;设置xml文件关联,setContentView(R.layout.activity_main);
2>新建xml文件如activity_main.xml;在里面设置控件
3>新建一个Activity文件如Second_Activity;重写onCreat方法;设置xml文件关联,setContentView(R.layout.secondview);
4>新建xml文件如secondview.xml;在里面设置控件
5>将新建的Activity文件都配置到Android.Manifest.xml文件中
6>重写第一个页面跳转到第二个页面的按钮的点击事件的方法
Intent intent = new Intent(MainActivity.this,SecondActivity.class);//第一个参数是上下文文件,第二个对象是目标文件
MainActivity.this.startActivity(intent);
1>MainActivity.java
package com.thinker.activitydemo; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; 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) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,Second_Activity.class); MainActivity.this.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; } }
2〉activity_main.xml
<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_alignParentLeft="true" android:text="启动第二个Activity" /> </RelativeLayout>
3〉Second_Activity.java
package com.thinker.activitydemo; import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class Second_Activity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.secondview); } }
4:secondview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="67dp" android:text="这是第二个Activity" /> </RelativeLayout>
5:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thinker.caculator" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.thinker.caculator.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.thinker.caculator.SecondActivity" ></activity> </application> </manifest>
二:有返回值的页面跳转
步骤描述:
已经有默认的MainActivity, 1:新建一个SecondActivity.java,新建一个second_activity.xml 2:重写SecondActivity.java的onclick方法,设置xml文件关联,配置AndroidManifest.xml文件,然后去设置两个xml的控件 3:在MainActivity.java文件中建立控件的对象并设置点击事件, bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(mContext, SecondActivity.class); /* * 第一个参数是intent对象 * 第二个参数是请求的标识 */ startActivityForResult(intent, 1); } }); 4: 在第二个页面为按钮设置点击事件,设置点击这个按钮的同时返回的结果 bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent data = new Intent(); /* * 第一个参数是key,第二个参数是resultCode */ data.putExtra("data", content); setResult(2,data); finish();//结束当前的页面,销毁当前页面,从而返回第一个页面 }; }); 5: 在第一个页面重写接收数据的onActivityResult方法 protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub /* * 通过 onActivityResult跳转,接收返回数据的方法 * requestCode:请求的标识 * resultCode:第二个页面回传的数据 */ super.onActivityResult(requestCode, resultCode, data); if(requestCode == 1 && resultCode == 2){ String content = data.getStringExtra("data");//因为 key是data tv.setText(content); } }
1: MainActivity.java方法:
package com.thinker.returnresultdemo; import android.app.Activity; import android.content.Context; 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 MainActivity extends Activity { private Button bt1; private Button bt2; private TextView tv; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1 = (Button) findViewById(R.id.button1); bt2 = (Button) findViewById(R.id.button2); tv = (TextView) findViewById(R.id.textView1); mContext = this; bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(mContext,SecondActivity.class); MainActivity.this.startActivity(intent); } }); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(mContext, SecondActivity.class); /* * 第一个参数是intent对象 * 第二个参数是请求的标识 */ startActivityForResult(intent, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub /* * 通过 onActivityResult跳转,接收返回数据的方法 * requestCode:请求的标识 * resultCode:第二个页面回传的数据 */ super.onActivityResult(requestCode, resultCode, data); if(requestCode == 1 && resultCode == 2){ String content = data.getStringExtra("data");//因为 key是data tv.setText(content); } } }
2: activity_main.xml
<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_alignParentLeft="true" android:layout_alignParentTop="true" android:text="第一种启动方式" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:text="第二种启动方式" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button2" android:layout_below="@+id/button2" android:layout_marginTop="18dp" android:gravity="center" android:text="TextView" /> </RelativeLayout>
3:SecondActivity.java
package com.thinker.returnresultdemo; 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 bt; private String content = "你好"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second_view); /* * 第二个页面什么时候给第一个页面回传数据 * 回传到第一个页面的实际上是一个Intent对象 */ bt = (Button) findViewById(R.id.button1); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent data = new Intent(); /* * 第一个参数是key,第二个参数是resultCode */ data.putExtra("data", content); setResult(2,data); finish();//结束当前的页面,销毁当前页面,从而返回第一个页面 }; }); } }
4:second_view.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="Button" /> </RelativeLayout>
5:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thinker.returnresultdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.thinker.returnresultdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.thinker.returnresultdemo.SecondActivity" android:label="@string/app_name" > </activity> </application> </manifest>