第39天~~又是JSON+动态增加按钮不是用XML

这篇的上一篇:https://ithelp.ithome.com.tw/articles/10283923

~又是JSON~~要把JSON练熟~
https://zh.wikipedia.org/wiki/JSON

这里要用logcat取出JSON的方法:

先建立新的Android档-

https://ithelp.ithome.com.tw/upload/images/20220211/20119035b9Ztc9ElQe.png
用logcat取出JSON的方法:

https://ithelp.ithome.com.tw/upload/images/20220211/20119035ulgVXx4pxk.png

package com.huang.json2;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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

        String json = String.format("{\"%s\":\"%s\"}","firstName","Ken");
        Log.d("Howard","Json"+json);
    }
}

使用Android内建套件
把错误"印"出来~

https://ithelp.ithome.com.tw/upload/images/20220211/20119035McsO7jPVIw.png

目前变这样
https://ithelp.ithome.com.tw/upload/images/20220211/20119035DLAa1zvt9G.png

然後再来改内容
https://ithelp.ithome.com.tw/upload/images/20220211/20119035FVtyit2m3e.png

package com.huang.json2;

import androidx.appcompat.app.AppCompatActivity;

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

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

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

        String json = String.format("{\"%s\":\"%s\"}","firstName","Ken");
        Log.d("Howard","Json"+json);

        try {
            JSONObject obj =new JSONObject(json);
        } catch (JSONException e) {
            Log.e("Howard","JSONException"+e);
        }
    }
}


再来取出数字:

https://ithelp.ithome.com.tw/upload/images/20220211/20119035CVSddrcJQl.png

跑一下取出KEN

https://ithelp.ithome.com.tw/upload/images/20220211/20119035SfhdzQqgUQ.png

package com.huang.json2;

import androidx.appcompat.app.AppCompatActivity;

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

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

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

        String json = String.format("{\"%s\":\"%s\"}","firstName","Ken");
        Log.d("Howard","Json"+json);

        try {
            JSONObject obj =new JSONObject(json);

            String firstName = obj.getString("firstName");
            Log.d("Howard","firstName:"+firstName);

        } catch (JSONException e) {
            Log.e("Howard","JSONException"+e);
        }
    }
}

动态增加按钮不是用XML

/images/emoticon/emoticon04.gif
开新档案-
XML档先改成-LinearLayout

https://ithelp.ithome.com.tw/upload/images/20220212/20119035g60jbfArF4.png

XML里面只有放...

命名- containerView
https://ithelp.ithome.com.tw/upload/images/20220212/20119035M1GKqwUsyY.png

然後用JAVA档-然後用JAVA档-
/images/emoticon/emoticon21.gif

package com.huang.dynamic_ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

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

        LinearLayout layout=findViewById(R.id.containerView);

        Button button = new Button(this);
        button.setText("存档");
        layout.addView(button);

    }
}

https://ithelp.ithome.com.tw/upload/images/20220212/20119035jT33tGAuRK.png

就可以长出这样~

然後再加入for回圈

https://ithelp.ithome.com.tw/upload/images/20220212/20119035PmmomlSUri.png

package com.huang.dynamic_ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

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

        LinearLayout layout=findViewById(R.id.containerView);

        Button button = new Button(this);
        button.setText("存档");
        layout.addView(button);

        for(int i=1;i <=10;i ++){
            CheckBox checkBox = new CheckBox(this);
            checkBox.setText("Item:" + i);
            layout.addView(checkBox);

        }

        

    }
}

就长成这样:
https://ithelp.ithome.com.tw/upload/images/20220212/20119035c8cU6stbMt.jpg

这篇的下一篇:https://ithelp.ithome.com.tw/articles/10283962


<<:  Android 逆向工程 - 不确定 App 是否有混淆,所以反组译看看 ( 4步骤 )

>>:  第40天~还是JSON

README,写在开赛第一天

去年写了跟自身专业 Android 题目,在每天固定输出文字是一种表达的练习。而今年持续分享自己的在...

[Day 27] 微探讨 Pure pipe 与 Impure pipe

今天要介绍的 Tip 是有关於 pipe 的 pure 与 impure,当没有任何额外的设定下,自...

Day19|【Git】开始使用分支 - git branch(基本常用指令)

学习完 git 的基本观念後,我们就来进入下一阶段,创建分支吧! 为什麽会需要分支呢? 分支的好处在...

Day02 - 【入门篇】Quick Start(2)

本系列文之後也会置於个人网站 昨天,已经完成了一部分配置,且也已经可以建立帐号并登入了。 不过,这...

[Part 4 ] Vue.js 的精随-元件 Slots

Slots ? 想要传入内容到子层中,slots 是另一种可以选择的方式,在 component 预...