zoukankan      html  css  js  c++  java
  • 【Android开发-8】生命周期,Activity中打开另外一个Activity

    前言:生命中有很多人陪伴自己走过一生中的某段旅程,仅仅是有些人仅仅是某阶段出现,有些人却陪伴自己非常久。就像小学、中学、高中、大学,那些以前以为会长久拥有的,当经历过天涯各地地忙碌于生活,或如意。或失意;渐渐地那些画面仅仅剩回顾。天涯各自安命,能在一起的就尽力珍惜吧,不在一起地就衷心地祝福,我们都须要一种姿态生活下去!Android中的Activity的生命中,我们常常须要打开另外一个Activity,即另外一个界面。这个可能出现的时间非常短暂,然后又回到主界面。可是这两个Activity都在各自的生命周期中经历了生命中的某个阶段。


    该项目在上篇项目中加入相关内容,进行演示。项目结构例如以下:


    第一步:在layout中加入另外一个界面布局,即activity_sub.xml,代码内容例如以下:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
     >
    
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
        <Button 
            android:id="@+id/close"
            android:layout_below="@id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/close" 
            />
    
    </RelativeLayout>

    当然主界面activity_sub.xml内容又一次布局下。代码例如以下:

    <RelativeLayout 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/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
        <Button 
            android:id="@+id/open"
            android:layout_below="@id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/open" 
            />
    
    </RelativeLayout>

    第二步:在values文件夹下的strings.xml中编写代码内容例如以下:

    <?

    xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TestActivity</string> <string name="hello_world">Test Activity life cycle</string> <string name="close">关闭</string> <string name="open">打开另外一个界面</string> </resources>


    注:这个文件里的东西是layout界面布局中要用到的东西;比方android:text="@string/open"中的open就是相应<string name="open">打开另外一个界面</string>,这个界面布局完后,程序界面button就会显示:打开另外一个界面


    第三步:在src中新建一个文件SubActivity.java,代码编写例如以下:

    package com.wyz.testactivity;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class SubActivity extends Activity {
    
    	private static final String TAG="SubActivity";
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_sub);
    		
    		Log.d(TAG, "生命周期--创建阶段(onCreate)");
    		
    		Button btn_close = (Button)findViewById(R.id.close);
    		
    		btn_close.setOnClickListener(new OnClickListener(){
    
    			@Override
    			public void onClick(View v) {
    				SubActivity.this.finish();	//关闭
    			}
    			
    		});
    	}
    	
    	protected void onStart()
    	{
    		super.onStart();
    		Log.d(TAG, "生命周期--開始阶段(onStart)");
    	}
    	
    	protected void onResume()
    	{
    		super.onResume();
    		Log.d(TAG, "生命周期--恢复阶段(onResume)");
    	}
    	protected void onPause()
    	{
    		super.onPause();
    		Log.d(TAG, "生命周期--暂停创建阶段(onPause)");
    	}
    	
    	protected void onStop()
    	{
    		super.onStop();
    		Log.d(TAG, "生命周期--停止创建阶段(onStop)");
    	}
    	
    	protected void onRestart()
    	{
    		super.onRestart();
    		Log.d(TAG, "生命周期--重新启动阶段(onRestart)");
    	}
    	
    	protected void onDestory()
    	{
    		super.onDestroy();
    		Log.d(TAG, "生命周期--销毁阶段(onDestory)");
    	}
    }

    接着我们也要改动下MainActivity.java,让它能够打开子界面:

    package com.wyz.testactivity;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
    	private static final String TAG="MainActivity";
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		Log.d(TAG, "生命周期--创建阶段(onCreate)");
    		
    		Button btn_open = (Button)findViewById(R.id.open);
    		
    		btn_open.setOnClickListener(new OnClickListener(){
    
    			@Override
    			public void onClick(View v) {
    				startActivity(new Intent(MainActivity.this, SubActivity.class));	//打开另外一个界面
    			}
    			
    		});
    	}
    	
    	protected void onStart()
    	{
    		super.onStart();
    		Log.d(TAG, "生命周期--開始阶段(onStart)");
    	}
    	
    	protected void onResume()
    	{
    		super.onResume();
    		Log.d(TAG, "生命周期--恢复阶段(onResume)");
    	}
    	protected void onPause()
    	{
    		super.onPause();
    		Log.d(TAG, "生命周期--暂停创建阶段(onPause)");
    	}
    	
    	protected void onStop()
    	{
    		super.onStop();
    		Log.d(TAG, "生命周期--停止创建阶段(onStop)");
    	}
    	
    	protected void onRestart()
    	{
    		super.onRestart();
    		Log.d(TAG, "生命周期--重新启动阶段(onRestart)");
    	}
    	
    	protected void onDestory()
    	{
    		super.onDestroy();
    		Log.d(TAG, "生命周期--销毁阶段(onDestory)");
    	}
    }
    

    最后一步:改动下AndroidManifest.xml,在</application>节点前加入内容例如以下:

     <activity  
                android:name=".SubActivity"  
                android:label="@string/app_name" >  
     </activity> 

    这个加入的作用是:让程序知道有另外一个界面存在了!

    注:android:name就是子界面的类名


    界面效果例如以下:





  • 相关阅读:
    真正的Java学习从入门到精通
    Java学习从入门到精通(1) [转载]
    Java Learning Path(三)过程篇
    Java Learning Path(五)资源篇
    Java Learning Path(四) 方法篇
    浅析Java语言中两种异常的差别
    JDK,JRE,JVM区别与联系
    JAVA敏捷开发环境搭建
    谈谈WEB开发中的苦大难字符集问题
    java读取clob字段的几种方法
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/6939579.html
Copyright © 2011-2022 走看看