您好,登錄后才能下訂單哦!
1,在activity_main.xml中添加一個(gè)按鈕
內(nèi)容:
<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="跳轉(zhuǎn)" /> </RelativeLayout>
2,新建一個(gè)跳轉(zhuǎn)的目標(biāo)頁(yè)面的xml文件:activity_second.xml,并且添加一個(gè)TextView
內(nèi)容:
<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="第二個(gè)activity" android:textColor="#ff0000" /> </RelativeLayout>
3,新建一個(gè)SecondActivity.java,把MainActivity里的內(nèi)容復(fù)制過(guò)去,改下這行內(nèi)容:
setContentView(R.layout.activity_second);
內(nèi)容:
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里實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
內(nèi)容:
package com.example.activity_tiaozhuan; //包名視具體情況自行改動(dòng) 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); //給按鈕注冊(cè)點(diǎn)擊事件 } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_activity: method(); //按鈕是R.id.bt_activity時(shí),調(diào)用method()方法來(lái)實(shí)現(xiàn)點(diǎn)擊的具體意圖 break; } } private void method() { Intent intent = new Intent(this,SecondActivity.class); //定義一個(gè)跳轉(zhuǎn)到SecondActivity的意圖 startActivity(intent); //啟動(dòng)意圖intent } }
5,在AndroidMainfest.xml中注冊(cè)SecondActivity,添加:
<activity
android:name="com.example.activity_tiaozhuan.SecondActivity" ></activity>
內(nèi)容:
<?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>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。