简单实现两个activity相互跳转

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

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context="com.example.android01.MainActivity" >

  10.     <TextView
  11.         android:id="@+id/MyText"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="wrap_content" />
  14.     
  15.     <Button
  16.         android:id="@+id/MyButton"
  17.         android:layout_width="match_parent"
  18.         android:layout_height="match_parent" />
  19. </RelativeLayout>


第一个activity:

点击(此处)折叠或打开

  1. package com.example.android01;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View; //import android.view.View.OnClickListener;
  6. import android.widget.Button;

  7. public class MainActivity extends Activity {

  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.         
  14.         Button myButton = (Button)findViewById(R.id.MyButton); //用findViewById获取Button控件的id
  15.         
  16.         myButton.setText("请点击!跳转界面");                    //设置Button控件上的Text
  17.         
  18.         myButton.setOnClickListener(new wang()); //绑定当前的Button  
  19.         
  20.     }
  21.     
  22.      class wang implements android.view.View.OnClickListener {  //构造一个内部类,并用Intent对象进行跳转
  23.         
  24.         
  25.         public void onClick(View v){
  26.             
  27.             Intent intent = new Intent();
  28.             intent.setClass(MainActivity.this,otherActivity.class);//从当前activity跳转到另一个activity
  29.             MainActivity.this.startActivity(intent);              
  30.             
  31.         }
  32.         
  33.     }
  34. }
第二个activity:


点击(此处)折叠或打开

  1. package com.example.android01;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.TextView;

  9. public class otherActivity extends Activity {
  10.    
  11.         @Override
  12.         protected void onCreate(Bundle savedInstanceState) {  
  13.             
  14.             super.onCreate(savedInstanceState);
  15.             setContentView(R.layout.activity_main);
  16.             
  17.             TextView Mytext = (TextView)findViewById(R.id.MyText);
  18.             Button myButton = (Button)findViewById(R.id.MyButton); //该activity中的操作和上面activity一样,不再赘述
  19.             myButton.setText("第一个Button");
  20.             Mytext.setText(R.string.lios);
  21.             myButton.setOnClickListener(new lios());
  22.         }
  23.         
  24.         class lios implements OnClickListener{
  25.             
  26.             public void onClick(View v){
  27.                 
  28.                 Intent intent = new Intent();
  29.                 intent.setClass(otherActivity.this,MainActivity.class);
  30.                 otherActivity.this.startActivity(intent);
  31.             }
  32.         }
  33.     }
上面已经完成了两个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最简单的应用,还有很多其他应用。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29876893/viewspace-1815036/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29876893/viewspace-1815036/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值