如何启动另一个Activity

--------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>标签

image

 

 

 

 

给 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

image

 

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));
            }
        });

    }
}

 

 

 

发送到手机,效果一览:

 

1                                         2

 

 

 

 

通过 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")));
            }
        });

    }
}

 

 

 

 

 

发送到手机,效果一览:

1                      3

 

 

 

【made by siwuxie095】

转载于:https://www.cnblogs.com/siwuxie095/p/6209241.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值