第10天~生命周期

每本安卓课本的第1步
https://ithelp.ithome.com.tw/upload/images/20220201/201190352ja7hpHbbA.png

用程序码来走一次:

1-贴两个xml档-(代做)

2-看下面的logcat看过滤器-只看想看的
https://ithelp.ithome.com.tw/upload/images/20220201/20119035ifmIKzIMXz.png

3-选最下面的

https://ithelp.ithome.com.tw/upload/images/20220201/20119035G5ZQUvoqJK.png

4-打内容-还没有开始打的样子

https://ithelp.ithome.com.tw/upload/images/20220201/20119035DKktanI7lA.png

5-打内容-有打的样子

https://ithelp.ithome.com.tw/upload/images/20220201/20119035fJzl0MLZFx.png

6-按OK
https://ithelp.ithome.com.tw/upload/images/20220201/20119035lq377qVpIs.png
7-目前还没有看到东西

https://ithelp.ithome.com.tw/upload/images/20220201/20119035IrytOWbQwp.png

8-activity_main.xml里面有四个按钮-要onclick
https://ithelp.ithome.com.tw/upload/images/20220201/20119035f9mJSiAAgI.png
没有把


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

</androidx.constraintlayout.widget.ConstraintLayout>

https://ithelp.ithome.com.tw/upload/images/20220201/20119035kQYzZX8Hr0.png

改成

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

</LinearLayout>

https://ithelp.ithome.com.tw/upload/images/20220201/20119035dbDRAdOwkI.png
改完後
https://ithelp.ithome.com.tw/upload/images/20220201/20119035tQLT4QtzKB.png

再去改成orientation="vertical"

https://ithelp.ithome.com.tw/upload/images/20220201/201190350Dc7pkcC02.png

再去改 layout_width="match_parent" 和 layout_height="wrap_content"

https://ithelp.ithome.com.tw/upload/images/20220201/20119035MznnZyqdfs.png

改上+左+右 内距-

https://ithelp.ithome.com.tw/upload/images/20220201/20119035fsjcB8BJvA.png

改text内容
https://ithelp.ithome.com.tw/upload/images/20220201/201190354DdUbMY39t.png

制定onClick

https://ithelp.ithome.com.tw/upload/images/20220201/20119035YoYtPfOFkr.png

绑定onClick
https://ithelp.ithome.com.tw/upload/images/20220201/20119035VCkqQjZvEn.png


9-到java档

https://ithelp.ithome.com.tw/upload/images/20220201/20119035U9g3e98F8m.png

从初始化开始.看到整个页面

https://ithelp.ithome.com.tw/upload/images/20220201/201190356iBlQRdeLW.png

package com.huang.mylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("lifecycle","Activity 初始化");
    }

    public void onClick(View view) {
    }
}

呼叫onstart方法是:

1.右键-

https://ithelp.ithome.com.tw/upload/images/20220201/20119035DRj0pHqdXI.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035kc8GfrknP7.png

2.选alt+insert
选onStart():void 一定要大小写符合

https://ithelp.ithome.com.tw/upload/images/20220201/20119035BV8jVWpiou.png

生出来~

https://ithelp.ithome.com.tw/upload/images/20220201/20119035NS4RDZcvwr.png

准备就绪 就是 启动

package com.huang.mylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("lifecycle","Activity 初始化");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("lifecycle","Activity 准备就绪");
    }

    public void onClick(View view) {
    }
}

https://ithelp.ithome.com.tw/upload/images/20220201/20119035kNAzttB7kf.png

再来也是右键选到onResume()

https://ithelp.ithome.com.tw/upload/images/20220201/201190352lhFwQsCA9.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035n7TypXBRRv.png

package com.huang.mylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("lifecycle","Activity 初始化");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("lifecycle","Activity 准备就绪");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("lifecycle","Activity 元件可互动操作的状态");
    }

    public void onClick(View view) {
    }
}

再来是onPause

https://ithelp.ithome.com.tw/upload/images/20220201/20119035bE0iJguYKb.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035SXjmYZueZb.png

package com.huang.mylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("lifecycle","Activity 初始化");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("lifecycle","Activity 准备就绪");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("lifecycle","Activity 元件可互动操作的状态");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("lifecycle","Activity 暂停");
    }

    public void onClick(View view) {
    }
}

再来是onRestart

https://ithelp.ithome.com.tw/upload/images/20220201/20119035u8eNlfqf3Y.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035TbeFUJuvHa.png


onStop

https://ithelp.ithome.com.tw/upload/images/20220201/20119035e5s33jRNTv.png


销毁-onDestroy
https://ithelp.ithome.com.tw/upload/images/20220201/201190353OwyhR9ewf.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035JBasfMVaJj.png

package com.huang.mylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("lifecycle","Activity 初始化");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("lifecycle","Activity 准备就绪");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("lifecycle","Activity 元件可互动操作的状态");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("lifecycle","Activity 暂停");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("lifecycle","Activity 恢复使用");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("lifecycle","Activity 元件停止使用,不显示");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("lifecycle","Activity 销毁");
    }

    public void onClick(View view) {
    }
}

执行模拟器看-
https://ithelp.ithome.com.tw/upload/images/20220201/20119035meKpzbny7J.png


用onClick来做按钮-用switch来看哪个按钮被按到
预计要放入网址:https://jzs2home.github.io/blog/admin/blogs.html

https://ithelp.ithome.com.tw/upload/images/20220201/20119035BY0niehLJG.png

package com.huang.mylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("lifecycle","Activity 初始化");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("lifecycle","Activity 准备就绪");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("lifecycle","Activity 元件可互动操作的状态");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("lifecycle","Activity 暂停");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("lifecycle","Activity 恢复使用");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("lifecycle","Activity 元件停止使用,不显示");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("lifecycle","Activity 销毁");
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1://执行某功能
                String url="";

        }        
    }
}

预计要放入网址:https://jzs2home.github.io/blog/admin/blogs.html

放入Intent指定+放条件:指定开启要看的动作

https://ithelp.ithome.com.tw/upload/images/20220201/20119035g1LHI1AGGb.png

设定Intent去找适合看URL的语法:

public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1://执行某功能
                String url="https://jzs2home.github.io/blog/admin/blogs.html";
                Intent web = new Intent(Intent.ACTION_VIEW);
                web.setData(Uri.parse(url));
                startActivity(web);
                break;

        }


第1个按钮程序码:

package com.huang.mylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("lifecycle","Activity 初始化");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("lifecycle","Activity 准备就绪");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("lifecycle","Activity 元件可互动操作的状态");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("lifecycle","Activity 暂停");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("lifecycle","Activity 恢复使用");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("lifecycle","Activity 元件停止使用,不显示");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("lifecycle","Activity 销毁");
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1://执行某功能
                String url="https://jzs2home.github.io/blog/admin/blogs.html";
                Intent web = new Intent(Intent.ACTION_VIEW);
                web.setData(Uri.parse(url));
                startActivity(web);
                break;

        }
    }
}

button2-要到新增跳页的画面-

https://ithelp.ithome.com.tw/upload/images/20220201/20119035hds792uLwt.png

新增跳页的画面page2-

https://ithelp.ithome.com.tw/upload/images/20220201/20119035OBTbjykLmI.png

等初始化完-

https://ithelp.ithome.com.tw/upload/images/20220201/20119035DGRwodpLyg.png


https://ithelp.ithome.com.tw/upload/images/20220201/20119035kjiVW3RKbu.png

 case R.id.button2://开启指定页面
                Intent page = new Intent(this,Page2.class);
                startActivity(page);
                break;

到第2按钮完整程序码:

package com.huang.mylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("lifecycle","Activity 初始化");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("lifecycle","Activity 准备就绪");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("lifecycle","Activity 元件可互动操作的状态");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("lifecycle","Activity 暂停");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("lifecycle","Activity 恢复使用");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("lifecycle","Activity 元件停止使用,不显示");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("lifecycle","Activity 销毁");
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1://执行某功能
                String url="https://jzs2home.github.io/blog/admin/blogs.html";
                Intent web = new Intent(Intent.ACTION_VIEW);
                web.setData(Uri.parse(url));
                startActivity(web);
                break;

            case R.id.button2://开启指定页面
                Intent page = new Intent(this,Page2.class);
                startActivity(page);
                break;


        }
    }
}

灰色的地方贴CODE看不见.用截图的~
https://ithelp.ithome.com.tw/upload/images/20220201/20119035WuARDeLsop.png

Intent是重点:


到第3按钮: 开启对话框-至少要有一个确定的按钮~

builder.setPositiveButton();是确定的按钮意思

https://ithelp.ithome.com.tw/upload/images/20220201/20119035yx5BkGYROS.png

https://ithelp.ithome.com.tw/upload/images/20220201/20119035XLy6CpMj2w.png

case R.id.button3://开启对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(this);//显示出对话框
                builder.setMessage("Are you OK?");//对话框内容
                builder.setPositiveButton("OK",null);//对话框按钮.之後甚麽都不做
                builder.show();//显示出来
                break;

到第4按钮:离开页面就-销毁本页面,因为onDestroy是系统设定.这里是手动.所以用finish();
https://ithelp.ithome.com.tw/upload/images/20220201/20119035YF1wOMhWMm.png

case R.id.button4://销毁本页面
                finish();
                break;

完整的code

package com.huang.mylifecycle;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("lifecycle","Activity 初始化");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("lifecycle","Activity 准备就绪");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("lifecycle","Activity 元件可互动操作的状态");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("lifecycle","Activity 暂停");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("lifecycle","Activity 恢复使用");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("lifecycle","Activity 元件停止使用,不显示");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("lifecycle","Activity 销毁");
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1://执行某功能
                String url="https://jzs2home.github.io/blog/admin/blogs.html";
                Intent web = new Intent(Intent.ACTION_VIEW);
                web.setData(Uri.parse(url));
                startActivity(web);
                break;

            case R.id.button2://开启指定页面
                Intent page = new Intent(this,Page2.class);
                startActivity(page);
                break;

            case R.id.button3://开启对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(this);//显示出对话框
                builder.setMessage("Are you OK?");//对话框内容
                builder.setPositiveButton("OK",null);//对话框按钮.之後甚麽都不做
                builder.show();//显示出来
                break;

            case R.id.button4://销毁本页面
                finish();
                break;
        }
    }
}

https://ithelp.ithome.com.tw/upload/images/20220201/20119035jyamNhB48g.png

开启模拟器-先从button4开始测试-销毁
https://ithelp.ithome.com.tw/upload/images/20220201/20119035RXGnh2fK33.png


从button3开始测试

https://ithelp.ithome.com.tw/upload/images/20220201/20119035uc3LEhnhfg.png

按OK
https://ithelp.ithome.com.tw/upload/images/20220201/20119035jlgvNPGQNt.png


从button2开始测试

https://ithelp.ithome.com.tw/upload/images/20220201/201190353Eneoe73Xj.png


从button1开始测试-开启网页

https://ithelp.ithome.com.tw/upload/images/20220201/20119035PoTQwRdhGr.png


https://ithelp.ithome.com.tw/upload/images/20220201/20119035NuSW7MEHFa.png

今天有产出东西的~
我去玩了一下github pages的产生0.0
https://jzs2home.github.io/blog/admin/blogs.html
只有静态网页


<<:  Day16颜色(CSS)

>>:  Day 11 - Using CKEditor + CKFinder WYSIWYG Editor with ASP.NET Web Forms C# 在网页嵌入 WYSIWYG 文字编辑器

D18: 工程师太师了: 第9.5话

工程师太师了: 第9.5话 杂记: 散热器可以帮助电脑排出废热,避免高温损坏电子零件 利用导热其片接...

乐观锁 vs. 悲观锁

ref : https://medium.com/dean-lin/真正理解资料库的悲观锁-vs-乐...

Day05 X Code Minimize & Uglify

从今天开始终於要正式进入介绍前端效能优化各种技巧的章节了,如果到今天还愿意继续坚持看下去的读者记得...

最有可能导致数据泄露的针对智能卡(smart cards)的攻击

-侧信道攻击 侧信道攻击(Side-channel attack) 只需在设备或系统附近放置天线、...

中阶魔法 - 提升 Hoisting

前情提要 艾草:「我们今天来提升一下吧!」 「不是每天都在提升魔力总量了吗?」 艾草:「不一样唷,今...