您的位置:首页 > 其它

简单实现两个activity相互跳转

2015-10-19 21:12 405 查看
下面简单实现两个activity之间相互跳转。我们首先要明白的是一个MyActivity就是一个类,而这个类继承Activity类。实现两个activity之间的跳转,则我们需要创建两个activity子类。
首先看下简单的布局文件:

<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="com.example.android01.MainActivity" >

<TextView
android:id="@+id/MyText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/MyButton"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

第一个activity:

package com.example.android01;
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 MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button myButton = (Button)findViewById(R.id.MyButton); //用findViewById获取Button控件的id

myButton.setText("请点击!跳转界面"); //设置Button控件上的Text

myButton.setOnClickListener(new wang()); //绑定当前的Button

}

class wang implements android.view.View.OnClickListener { //构造一个内部类,并用Intent对象进行跳转

public void onClick(View v){

Intent intent = new Intent();
intent.setClass(MainActivity.this,otherActivity.class);//从当前activity跳转到另一个activity
MainActivity.this.startActivity(intent);

}

}
}

第二个activity:

package com.example.android01;

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 otherActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView Mytext = (TextView)findViewById(R.id.MyText);
Button myButton = (Button)findViewById(R.id.MyButton); //该activity中的操作和上面activity一样,不再赘述
myButton.setText("第一个Button");
Mytext.setText(R.string.lios);
myButton.setOnClickListener(new lios());
}

class lios implements OnClickListener{

public void onClick(View v){

Intent intent = new Intent();
intent.setClass(otherActivity.this,MainActivity.class);
otherActivity.this.startActivity(intent);
}
}
}
上面已经完成了两个activity代码部分,但是在运行Android程序,会报错,因为otherActivity没有在AndroidMainfest.xml中注册,关于该文件的作用,这里不作说明。
下面注册新建activity的信息:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.android01"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="14"

        android:targetSdkVersion="18" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".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=".otherActivity" >

        </activity>

    </application>

</manifest>

然后执行程序,出现我们想要的结果。上面只是应用Intent最简单的应用,还有很多其他应用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: