展会信息港展会大全

android之两个activity跳转的代码
来源:互联网   发布日期:2016-01-14 10:22:24   浏览:2872次  

导读:实现俩个activity页面的跳转1 首先建个android工程2 在layout文件下main xml布局文件, 输入以下代码LinearLayout xmlns:android=http: schemas android com apk res android xmlns:tools=http: schem ...

实现俩个activity页面的跳转

1.首先建个android工程

2.在layout文件下main.xml布局文件, 输入以下代码

<LinearLayout 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" >

<Button

android:name="@+id/Button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="跳转" />

</LinearLayout>

3.在layout文件夹下新建个android的xml布局文件,命名为second,输入以下内容:

<LinearLayout 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" >

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="第二个activity"

/>

</LinearLayout>

4.建一个secondActivity.java,把mainActivity里的内容复制过去,改下这行内容:

setContentView(R.layout.second);

5.在mainActivity里输入以下代码:

import android.os.Bundle;

import android.app.Activity;

import android.content.DialogInterface;

import android.content.Intent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button but=(Button)findViewById(R.id.button1);

but.setOnClickListener(this);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent=null;

switch (v.getId()) {

case R.id.button1:

intent =new Intent(this,secondactivity.class);

startActivity(intent);

break;

default:

break;

}

}

}

6.修改Mainfest.xml文件,在application标签里输入以下内容:

<activityname=".secondActivity"/>

赞助本站

人工智能实验室

相关热词: activity跳转 android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港