鱼C论坛

 找回密码
 立即注册
查看: 2597|回复: 0

[学习笔记] android编程2.4

[复制链接]
发表于 2017-9-20 11:16:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
活动生命周期其实就是程序执行的各种过程,程序刚执行的时候叫oncreate,结束叫ondestroy。在这些函数里由我们来写代码初始化或者释放变量空间。以下为示意图: 1293257671Omb1lL04.png
首先在菜单里点close project把原来的项目关闭,我们再新建一个empty activity的项目,然后把activity_main.xml修改为:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     android:orientation="vertical"
  5.     xmlns:tools="http://schemas.android.com/tools"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     tools:context="com.example.xinwei.lifecycle.MainActivity">

  9.     <Button
  10.         android:id="@+id/button_1"
  11.         android:layout_width="match_parent"
  12.         android:layout_height="wrap_content"
  13.         android:text="button1" />
  14.     <Button
  15.         android:id="@+id/button_2"
  16.         android:layout_width="match_parent"
  17.         android:layout_height="wrap_content"
  18.         android:text="button2" />

  19. </LinearLayout>
复制代码

然后在mainactivity.java旁边新建两个activity文件,一个叫NormalActivity.java,一个叫DialogActivity.java两个empty activity,把两个layout文件分别命名为normal_layout和dialog_layout,然后把androidmanifest.xml文件修改为以下代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.example.xinwei.lifecycle">

  4.     <application
  5.         android:allowBackup="true"
  6.         android:icon="@mipmap/ic_launcher"
  7.         android:label="@string/app_name"
  8.         android:roundIcon="@mipmap/ic_launcher_round"
  9.         android:supportsRtl="true"
  10.         android:theme="@style/AppTheme">
  11.         <activity android:name=".MainActivity">
  12.             <intent-filter>
  13.                 <action android:name="android.intent.action.MAIN" />

  14.                 <category android:name="android.intent.category.LAUNCHER" />
  15.             </intent-filter>
  16.         </activity>
  17.         <activity android:name=".NormalActivity" />
  18.         <activity
  19.             android:name=".DialogActivity"
  20.             android:theme="@style/Theme.AppCompat.Dialog" />
  21.     </application>

  22. </manifest>
复制代码

然后把mainactivity.java文件修改为:
  1. package com.example.xinwei.lifecycle;

  2. import android.content.DialogInterface;
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.Toast;

  10. public class MainActivity extends AppCompatActivity {
  11.     private static String TAG="MainActivity";
  12.     private MyListener myListener;
  13.     private Intent intent1;
  14.     private Intent intent2;
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         Log.d(TAG,"onCreate");
  19.         setContentView(R.layout.activity_main);
  20.         Button btn1 = (Button)findViewById(R.id.button_1);
  21.         Button btn2 = (Button)findViewById(R.id.button_2);
  22.         intent1 = new Intent(this,NormalActivity.class);
  23.         intent2 = new Intent(this,DialogActivity.class);
  24.         myListener = new MyListener();
  25.         btn1.setOnClickListener(myListener);
  26.         btn2.setOnClickListener(myListener);
  27.     }

  28.     protected class MyListener implements View.OnClickListener{
  29.         @Override
  30.         public void onClick(View view) {
  31.             switch(view.getId()){
  32.                 case R.id.button_1:
  33.                     startActivity(intent1);
  34.                     break;
  35.                 case R.id.button_2:
  36.                     startActivity(intent2);
  37.             }
  38.         }
  39.     }

  40.     @Override
  41.     protected void onStart() {
  42.         super.onStart();
  43.         Log.d(TAG,"onStart");
  44.     }

  45.     @Override
  46.     protected void onResume() {
  47.         super.onResume();
  48.         Log.d(TAG,"onResume");
  49.     }

  50.     @Override
  51.     protected void onPause() {
  52.         super.onPause();
  53.         Log.d(TAG,"onPause");
  54.     }

  55.     @Override
  56.     protected void onStop() {
  57.         super.onStop();
  58.         Log.d(TAG,"onStop");
  59.     }

  60.     @Override
  61.     protected void onDestroy() {
  62.         super.onDestroy();
  63.         Log.d(TAG,"onDestroy");
  64.     }

  65.     @Override
  66.     protected void onRestart() {
  67.         super.onRestart();
  68.         Log.d(TAG,"onRestart");
  69.     }
  70. }
复制代码
然后开启程序在logcat下输入框输入MainActivity就可以看到我们写的日志,这需要你自己实验我这里就不说了,如图:
adasda.png
效果图:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 18:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表