Android应用程序中的多个Activity的显示创建和调用[通俗易懂]

Android应用程序中的多个Activity的显示创建和调用

大家好,又见面了,我是全栈君。

Android应用程序中的多个Activity的显示创建和调用[通俗易懂]Android应用程序中的多个Activity的显示创建和调用[通俗易懂]

Android应用程序中的多个Activity的显示创建和调用[通俗易懂]Android应用程序中的多个Activity的显示创建和调用[通俗易懂]

Android应用程序中的多个Activity的显示创建和调用[通俗易懂]

布局文件:

<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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="openActivity"
        android:text="开启第二个Activity" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="openSystemActivity"
        android:text="开启系统的Activity" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="checkConnection"
        android:text="检測网络状态" />

</LinearLayout>

主Activity的代码

package com.examp.manyactivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

/**
 * 案例演示的是显示的激活Activity
 * 
 * @author MartinDong
 * 
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	/**
	 * 用户想要打开第二个界面的时候
	 * 
	 * @param view
	 */
	public void openActivity(View view) {
		// 创建意图对象
		Intent intent = new Intent();
		// 方便调用setComponent与一个明白的类名。

// 相当于创建了一个新的组件 // 会话位置|指定要激活的详细的Activity intent.setClassName(this, "com.examp.manyactivity.SecondActivity"); // 另外一种方式,是在创建意图对象的时候进行指定Activity // Intent intent2 = new Intent(this, SecondActivity.class); // 激活一个Activity startActivity(intent); } /** * 开启系统中的Activity<br> * 案例演示的是开启图库的Activity * * @param view */ public void openSystemActivity(View view) { /* * 05-31 07:42:44.581: I/ActivityManager(150): START * {act=android.intent.action.MAIN * cat=[android.intent.category.LAUNCHER] flg=0x10200000 * cmp=com.android.gallery/com.android.camera.GalleryPicker u=0} from * pid 282 */ Intent intent = new Intent(); intent.setClassName("com.android.gallery", "com.android.camera.GalleryPicker"); startActivity(intent); } /** * 检測网路状态 * * @param view */ public void checkConnection(View view) { /* * 05-31 08:03:01.848: I/ActivityManager(150): START * {act=android.intent.action.MAIN cmp=com.android.settings/.SubSettings * (has extras) u=0} from pid 306 因为这里4.0的网络的管理须要传入附加数据,本功能使用2.3的虚拟机<br> * 05-31 08:05:47.072: I/ActivityManager(61): Starting: Intent { * act=android.intent.action.MAIN * cmp=com.android.settings/.WirelessSettings } from pid 168 */ // 检測网路的连接状态 // 创建连接管理对象 ConnectivityManager cm = (ConnectivityManager) this .getSystemService(Context.CONNECTIVITY_SERVICE); // 须要的权限 android.Manifest.permission.ACCESS_NETWORK_STATE // 获取网络的连接信息 NetworkInfo info = cm.getActiveNetworkInfo(); // 假设没有不论什么的网络信息info为null; if (info != null && info.isConnected()) { Toast.makeText(this, "网络可用......", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "网不可用,请设置......", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings"); startActivity(intent); } }}


第二个Activity文件:

package com.examp.manyactivity;

import android.app.Activity;
import android.os.Bundle;

/**
 * 自己定义的Activity<br>
 * 必需要继承Activity<br>
 * Activity是系统的四大组件之中的一个<br>
 * 操作系统想要找到Activity就必须在清单文件AndroidManifest.xml进行注冊<br>
 * 
 * 
 * @author MartinDong
 * 
 */
public class SecondActivity extends Activity {

	/**
	 * 一般都会重写的方法,用途大都是初始化一些数据,和程序的界面<br>
	 * Activity创建的时候进行调用
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置显示的布局
		setContentView(R.layout.activity_tow);

	}

}


第二个Activity相应的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/ratingBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckedTextView
        android:id="@+id/checkedTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckedTextView" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="246dp"
        android:layout_height="match_parent" />

</LinearLayout>


清单文件的配置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.examp.manyactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <!-- icon:指定应用程序的图标;label:指定应用程序的名称; -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- Activity的注冊 -->
        <!-- 假设Activity不进行icon,label的设置,那么将默认的使用应用application的icon,label 的设置 -->
        <!-- name指定的是布局文件相应的Activity类 -->
        <activity
            android:name="com.examp.manyactivity.MainActivity"
            android:label="@string/app_name" >

            <!--  -->
            <intent-filter>

                <!-- 告诉Android的系统这是应用的主界面 -->
                <action android:name="android.intent.action.MAIN" />
                <!-- 告诉Android的系统创建一个应用图标 -->
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.examp.manyactivity.SecondActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>


注:本案例的网络查看状态仅仅能在2.3的模拟器上使用;

Demo源代码下载:

http://download.csdn.net/detail/u011936142/7429455

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115830.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MixMatch论文学习笔记

    MixMatch论文学习笔记项目内容论文名MixMatch:AHolisticApproachtoSemi-SupervisedLearning作者DavidBerthelot,NicholasCarlini,IanGoodfellow,AvitalOliver,NicolasPapernot,ColinRaffel主要内容发表时间2019年Abstract

    2025年5月31日
    3
  • pycharm,pip3安装包失败解决,DB navigator 安装「建议收藏」

    pycharm,pip3安装包失败解决,DB navigator 安装「建议收藏」https://blog.csdn.net/TyuansushiT/article/details/81836732

    2022年8月25日
    8
  • 真正免费的国外PHP建站空间

    真正免费的国外PHP建站空间最近在学习wordpress,找了一个挺不错的国外免费建站空间:http://www.000webhost.com/582659.html空间属性:空间容量 1500MB数据流量 100GB/月附加域名  5个子域名   5个E-mail地址  5个MySQL数据库  2个

    2022年9月21日
    2
  • android 抛出FileNotFoundException异常

    android 抛出FileNotFoundException异常大家都知道,Android6.0中,某些权限属于ProtectedPermission,例如:读写手机存储权限,仅仅在AndroidManifest.xml中申明是无法真正获取到权限的,打开手机的权限管理页面,我们可以看见,读写手机存储权限栏是一个问号,这意味着App并未获取到该权限。这是访问手机存储时,会报出类似下面的错误:java.io.FileNotFoundExcept…

    2025年6月26日
    2
  • DLL文件反编译(附:工具下载链接)

    DLL文件反编译(附:工具下载链接)前几天写程序的时候电脑突然坏了,代码没有提交,已经更新过了,也就是说写的东西,除了DLL文件之外没别的东西了,代码全都没了,突然灵光一闪,想到了反编译。说干就干。百度上搜了下反编译工具,发现一款名叫“Reflector”的歪果反编译文件的效果不错,接下来,下载,为了方便大家,我直接放在百度云里面了:链接:https://pan.baidu.com/s/1PfxKxKp57pTYnSR3ThlOIg提取码:ohx2复制这段内容后打开百度网盘手机App,操作更方便哦下载下来之后,解压完是这样的:

    2025年7月4日
    5
  • windowshello指纹识别器_win10指纹驱动安装失败

    windowshello指纹识别器_win10指纹驱动安装失败首先,需要确认你的电脑安装有指纹识别设备,有的笔记本自带的就是指纹扫描器(例如MS酋长的惠普248G1笔记本),没有的可以考虑加装外设型的指纹识别设备。并且确认已经安装了指纹识别设备的驱动程序,方法是打开“设备管理器”,在其中查看有无“生物识别设备”。如图:然后进入“设置–帐户–登录选项”,即可在右侧空格中找到“WindowsHello”设置项。如图:点击“指纹”下的“设置”按钮,会…

    2022年8月10日
    21

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号