如何启动另一个Activity

来源:互联网 发布:批量注册淘宝号 编辑:程序博客网 时间:2024/06/06 09:49

--------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】

0 0