简单实现两个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最简单的应用,还有很多其他应用。
首先看下简单的布局文件:
<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最简单的应用,还有很多其他应用。
相关文章推荐
- 开发手机端API总结
- OpenGL3.3教程-----Shaders Are Coming
- Copy List with Random Pointer
- 5-7 12-24小时制
- 文件句柄、文件描述符与进程和多线程的那些事
- 去除UC浏览器点击的默认样式
- 形上之学
- 数据结构基础 0.前言
- eclipse安装android sdk后工具栏没有显示图标
- android内核剖析 创建窗口过程读书笔记
- Android蓝牙 打开 关闭 与 搜索
- POJ1511 Invitation Cards
- Java事务的相关概念
- [LeetCode]Majority Element
- 资本寒冬,互联网公司应该如何对待人才招聘
- 单例模式——创建型模式01
- UVA10617
- 2. select下拉框获取选中的值
- 黑马程序员——java学习日记一
- 距离和相似度度量