简单实现两个activity相互跳转

下面简单实现两个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的信息:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.example.android01"
  4.     android:versionCode="1"
  5.     android:versionName="1.0" >

  6.     <uses-sdk
  7.         android:minSdkVersion="14"
  8.         android:targetSdkVersion="18" />

  9.     <application
  10.         android:allowBackup="true"
  11.         android:icon="@drawable/ic_launcher"
  12.         android:label="@string/app_name"
  13.         android:theme="@style/AppTheme" >
  14.         <activity
  15.             android:name=".MainActivity"
  16.             android:label="@string/app_name" >
  17.             <intent-filter>
  18.                 <action android:name="android.intent.action.MAIN" />

  19.                 <category android:name="android.intent.category.LAUNCHER" />
  20.             </intent-filter>
  21.         </activity>
  22.         <activity android:name=".otherActivity" >
  23.         </activity>
  24.     </application>

  25. </manifest>
然后执行程序,出现我们想要的结果。上面只是应用Intent最简单的应用,还有很多其他应用。


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值