1,在activity_main.xml中添加一个按钮
内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container_1" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.activity_tiaozhuan.MainActivity" tools:ignore="MergeRootFrame" > <Button android:id="@+id/bt_activity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="跳转" /> </RelativeLayout>
2,新建一个跳转的目标页面的xml文件:activity_second.xml,并且添加一个TextView
内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container_2" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.activity_tiaozhuan.MainActivity" tools:ignore="MergeRootFrame" > <TextView android:id="@+id/tv_activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="第二个activity" android:textColor="#ff0000" /> </RelativeLayout>
3,新建一个SecondActivity.java,把MainActivity里的内容复制过去,改下这行内容:
setContentView(R.layout.activity_second);
内容:
package com.example.activity_tiaozhuan; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; public class SecondActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } }
4,在MainActivity里实现页面跳转
内容:
package com.example.activity_tiaozhuan; //包名视具体情况自行改动 import android.support.v7.app.ActionBarActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends ActionBarActivity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.bt_activity); //查找到按钮bt_activity bt.setOnClickListener(this); //给按钮注册点击事件 } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_activity: method(); //按钮是R.id.bt_activity时,调用method()方法来实现点击的具体意图 break; } } private void method() { Intent intent = new Intent(this,SecondActivity.class); //定义一个跳转到SecondActivity的意图 startActivity(intent); //启动意图intent } }
5,在AndroidMainfest.xml中注册SecondActivity,添加:
<activity
android:name="com.example.activity_tiaozhuan.SecondActivity" ></activity>
内容:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activity_tiaozhuan" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.activity_tiaozhuan.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.example.activity_tiaozhuan.SecondActivity" ></activity> </application> </manifest>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。