--------siwuxie95

首先为res->layout下my_layout.xml 的Design添加一个Button,进入Text,

android:text 修改为:启动另一个Activity

android:id 修改为:btnStartAnotherAty

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:text="启动另一个Activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnStartAnotherAty" />
</LinearLayout>

在java->MainActivity.java主程序中添加 findViewById(R.id.btnStartAnotherAty),设置一个

事件监听器  setOnClickListener(new OnClickListener…),自动变为如下代码:

package com.example.siwux.mainactivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(R.layout.my_layout); findViewById(R.id.btnStartAnotherAty).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//在onClick()中启动另一个Activity
}
}); }
}

在 java 上右键 new 一个 Activity->Empty Activity,命名为AnotherAty,

发现:不仅生成了 AnotherAty.java 类文件,还生成了布局文件 activity_another_aty.xml,

同时AndroidManifest.xml中也同步添加了<Activity>…</Activity>标签

给 activity_another_aty.xml 的Design添加一个TextView,进入Text:

android:text 修改为:这是另一个Activity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_another_aty"
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.siwux.mainactivity.AnotherAty"> <TextView
android:text="这是另一个Activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="19dp"
android:id="@+id/textView" />
</RelativeLayout>

回到MainActivity.java,为 onClick() 添加代码:

这里使用一个 startActivity() 的API,创建一个 new Intent() 实例,需要的是Context,Class的参数,

用 MainActivity.this 访问当前Context,启动的是AnotherAty.class

package com.example.siwux.mainactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(R.layout.my_layout); findViewById(R.id.btnStartAnotherAty).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//在onClick()中启动另一个Activity
startActivity(new Intent(MainActivity.this,AnotherAty.class));
}
}); }
}

发送到手机,效果一览:

                                        

通过 startActivity() 还可以启动一个页面,以手机版简版 Baidu 的页面(https://m.baidu.com/?pu=sz@1321_480)为例:

package com.example.siwux.mainactivity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(R.layout.my_layout); findViewById(R.id.btnStartAnotherAty).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//在onClick()中启动另一个Activity
// startActivity(new Intent(MainActivity.this,AnotherAty.class));
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://m.baidu.com/?pu=sz@1321_480")));
}
}); }
}

发送到手机,效果一览:

                     

【made by siwuxie095】

如何启动另一个Activity的更多相关文章

  1. 启动另外一个activity,并返回结果

    欢迎大家光临我的小店:http://clkk.taobao.com 大致步骤: 1.启动另外一个Activity,这里称子Activity: 2.子Activity通过setResult方法设置返回结 ...

  2. 【安卓面试题】在一个Activity启动另一个Activity和在Service中启动一个Activity有什么区别

    在Activity中可以直接使用Intent启动另一个Activity 显式Intent intent = new Intent(context, activity.class) 隐式 Intent ...

  3. Android应用开发学习之启动另外一个Activity

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一个Activity可以启动另外一个Activity,以实现比较复杂的功能,我们来看一个例子,其运行效果如下图所示: ...

  4. 【Android Developers Training】 4. 启动另一个Activity

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  5. 安卓-启动下一个Activity

    Intent intent = new Intent(NotebookActivity.this, MessageActivity.class); startActivity(intent); 通过意 ...

  6. 4) 十分钟学会android--建立第一个APP,启动另一个Activity

    在完成上一课(建立简单的用户界面)后,我们已经拥有了显示一个activity(一个界面)的app(应用),该activity包含了一个文本字段和一个按钮.在这节课中,我们将添加一些新的代码到MyAct ...

  7. Android官方教程翻译(4)——启动另一个Activity

    Starting Another Activity 启动另一个Activity PREVIOUSNEXT THIS LESSON TEACHES YOU TO 这节课教你 1.   Respond t ...

  8. Android中点击按钮启动另一个Activity以及Activity之间传值

    场景 点击第一个Activity中的按钮,启动第二个Activity,关闭第二个Activity,返回到第一个Activity. 在第一个Activity中给第二个Activity传递值,第二个Act ...

  9. 安卓入门 使用android创建一个项目 从启动activity中响应按钮事件 启动另一个activity 并传递参数

    启动android studio创建一个新项目 public void sendMessage(View view){ Intent intent=new Intent(this,DispalyMes ...

随机推荐

  1. Zookeeper安装指南

    第一步:修改conf目录下面的 zoo_sample.cfg修改为zoo.cfg tickTime=2000 # The number of ticks that the initial # sync ...

  2. Spring AOP 简单理解

    AOP技术即(面向切面编程)技术是在面向对象编程基础上的发展,AOP技术是对所有对象或一类对象编程.核心是在不增加代码的基础上,还增加了新的功能.AOP编程在开发框架本身用的比较多,而实际项目中,用的 ...

  3. MySQL日志恢复误删记录

    1.查询日志是否开启 show variables like"log_"; 2.查询是用的哪个日志文件 show master status; 3.定位是在什么时间误删的 /usr ...

  4. MapReduce格式与类型

    MapReduce Types MapReduce是一个简单的数据处理模型,map与reduce的输入和输出类型都为key-value形式的键值对. map: (K1, V1) → list(K2, ...

  5. 32. Path Sum && Path Sum II

    Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if ...

  6. SQL中使用WITH AS提高性能,使用公用表表达式(CTE)简化嵌套SQL

    一.WITH AS的含义     WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候, ...

  7. J-Link clone问题

    在上一篇<修复山寨版的J-Link>,中已经介绍了恢复的步骤. 但是,在使用J-Link驱动(V4.94J)升级后,会出现下面情况. The connected emulator is a ...

  8. Functional testing - python, selenium and django

    Functional testing  - python selenium django - Source Code : from selenium import webdriverfrom sele ...

  9. setTimeout

    setTimeout(function () { $('#successTip').hide(); location = location; }, 3000);

  10. linker command failed with exit code 1 (use -vto see invocation)

    报这样的错误可能是同一个.m文件同时存在,要先把你新添加的.m文件彻底删除 Move to Trash 点击这个删除.然后clear一下,再在重新添加你所需要的文件即可解决.这次添加不要推进来,需要在 ...