iT邦幫忙

2021 iThome 鐵人賽

DAY 10
0
Mobile Development

就是從無到有寫app系列 第 10

第10天~生命週期

  • 分享至 

  • xImage
  •  

每本安卓課本的第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
只有靜態網頁


上一篇
第9天~接續第2頁-
下一篇
第11天~
系列文
就是從無到有寫app31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
7_chun
iT邦新手 5 級 ‧ 2021-09-29 08:02:08

從你去年的文就知道你是來亂的了 別再鬧了好嗎

Tzu iT邦研究生 5 級 ‧ 2021-09-29 23:30:51 檢舉

0.0我後來有修正了~ 大大您有回頭去看modern wed嗎?都有更新了喔~
因為實在是太繁忙~只能先填~反正是對自己的激勵~
我後來都有改成正常的文了喔~如果不喜歡就不要看就好~
反正也沒有人會看0.0

我要留言

立即登入留言