iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
Mobile Development

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

第5天~

2021/1/25再更新:

因為每個電腦配置都不同,所以要移動時就是要用java和xml檔COPY

https://ithelp.ithome.com.tw/upload/images/20220125/20119035Reibm5Jynv.png


https://ithelp.ithome.com.tw/upload/images/20220125/20119035tnTinJssFl.png

用的方法-
1.開新空專案跑完
2.找到對應的檔案CTRL C+CTRL V
3.要.java和.xml檔都貼完才可以消失紅
https://ithelp.ithome.com.tw/upload/images/20220125/20119035daOiiD4NKO.png


https://ithelp.ithome.com.tw/upload/images/20220125/20119035DdqytcLPKt.png


覺得最麻煩的就是不斷更新的模擬器debug

記得老師有交代自己寫的APP不要用自己在用的手機測..
但是TOAST模擬器真的不能....
https://www.google.com/search?sxsrf=AOaemvJ4BLMRazL6ZIXVhmwWDU1yQ6LJ9Q%3A1642918325626&lei=tfHsYazlJYv30AT2_YLwCQ&q=Android%20studio%20Toast%20%E9%A1%AF%E7%A4%BA%20%E4%B8%8D%20%E5%87%BA%E4%BE%86&ved=2ahUKEwjsk8bZm8f1AhWLO5QKHfa-AJ4QsKwBKAB6BAg0EAE&biw=1536&bih=722&dpr=1.25

雖然做久了就習慣
https://ithelp.ithome.com.tw/upload/images/20220125/20119035a1P6sYevTc.png

要全部有id也可以但是下面一定要有
https://ithelp.ithome.com.tw/upload/images/20220125/201190354JnZKLknHS.png

調字大小.字的大小單位是sp:

https://ithelp.ithome.com.tw/upload/images/20220125/20119035Jgan0Ke7xJ.png

要先用RadioGroup 再用 RadioButton
https://ithelp.ithome.com.tw/upload/images/20220125/20119035hMmxrtyG3r.png

要先用RadioGroup-不好綁要放大點

https://ithelp.ithome.com.tw/upload/images/20220125/20119035i38AotGmeq.png


再用 RadioButton-一定有縮排-用RadioGroup只能選定一個

https://ithelp.ithome.com.tw/upload/images/20220125/20119035vAMMsZXfTQ.png


ConstraintLayout-是預設一個一個排的-沒有縮排

所以下面容器用LinearLayout 來選要直著排..還是橫著排..
先拉好在下面(不好調~><)

https://ithelp.ithome.com.tw/upload/images/20220125/201190359a0btw0SoU.png

用旁邊的Layout-Constraint Widget去調旁邊的距離是最好的

https://ithelp.ithome.com.tw/upload/images/20220125/20119035J5o4LlRPw0.png


RadioButton單選-要有id-拉到旁邊

https://ithelp.ithome.com.tw/upload/images/20220125/20119035np6d1Mi76S.png

https://ithelp.ithome.com.tw/upload/images/20220125/20119035ebI5syyOn2.png

再加入button

再加入textview

https://ithelp.ithome.com.tw/upload/images/20220125/20119035elSSutRPEH.png

https://ithelp.ithome.com.tw/upload/images/20220126/20119035CsqCb0mVz7.png


用radioGroup去管理radioButton
所以要先按radioGroup-一定要有id

https://ithelp.ithome.com.tw/upload/images/20220126/20119035CojSdHh1LR.png


重構-ok才能變
https://ithelp.ithome.com.tw/upload/images/20220126/20119035YIDTeAudIU.png

再來是radioButton的id

https://ithelp.ithome.com.tw/upload/images/20220126/20119035SZEP7apwo6.png

從code來改

https://ithelp.ithome.com.tw/upload/images/20220126/20119035UNrq7gF8qg.png

https://ithelp.ithome.com.tw/upload/images/20220126/20119035PyxWO9XIu5.png

新增一個按鈕-一個完整xml

<RadioButton
            android:id="@+id/r2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="女士" />

https://ithelp.ithome.com.tw/upload/images/20220126/20119035y36AxzOOSc.png

看面板
https://ithelp.ithome.com.tw/upload/images/20220126/20119035tLEbY7Npi4.png


要改方向-orientation
https://ithelp.ithome.com.tw/upload/images/20220126/20119035G3p2jko0cd.png

改完的樣子

https://ithelp.ithome.com.tw/upload/images/20220126/201190354ZmaklhEQK.png


CheckBox複選-要有id

linearlayout 設定寬度

https://ithelp.ithome.com.tw/upload/images/20220127/20119035ga1CCmBa13.png

wrap_content設定高度

https://ithelp.ithome.com.tw/upload/images/20220127/20119035CxOveTQzR6.png

wrap_content

wrap 翻譯過來是包裹,conten是內容。那麼這個就是包裹內容的意思,也就是說你的控制元件裡面的內容有多大,這個控制元件就有多大。

https://ithelp.ithome.com.tw/upload/images/20220127/20119035hd4odROs9K.png

linearLayout設定內距Padding 讓他留邊邊-左/右
https://ithelp.ithome.com.tw/upload/images/20220127/201190357A3cTS92VB.png

https://ithelp.ithome.com.tw/upload/images/20220127/20119035XkX5EA1iED.png

https://ithelp.ithome.com.tw/upload/images/20220127/201190355ceFDtck3y.png


開始寫裡面的id

https://ithelp.ithome.com.tw/upload/images/20220127/20119035fJcOrbhw2Y.png

https://ithelp.ithome.com.tw/upload/images/20220127/20119035CKHFucW5I0.png


https://ithelp.ithome.com.tw/upload/images/20220127/201190357axhj66aRu.png


button-確認送出

https://ithelp.ithome.com.tw/upload/images/20220127/20119035qlTBqYaN6I.png

改字的大小

https://ithelp.ithome.com.tw/upload/images/20220127/201190352sbXiFl6xq.png

制定 onClick

https://ithelp.ithome.com.tw/upload/images/20220127/20119035R1GK1vWJvn.png


開始綁定 onClick

https://ithelp.ithome.com.tw/upload/images/20220127/20119035EVLhd0PHvX.png

程式碼變
https://ithelp.ithome.com.tw/upload/images/20220127/20119035rXynk4DDMV.png

有時候剛改完 紅色 不會馬上變黃色


最後顯示的TextView-要有id

https://ithelp.ithome.com.tw/upload/images/20220127/20119035DjnFREEOBn.png

目前顯示
https://ithelp.ithome.com.tw/upload/images/20220127/201190357CmD79bbXz.png

模擬器再重動

https://ithelp.ithome.com.tw/upload/images/20220127/20119035q7zL4OUPBd.png

改字的大小

https://ithelp.ithome.com.tw/upload/images/20220127/20119035j7OrbwS9Yp.png


要注意import開始寫

https://ithelp.ithome.com.tw/upload/images/20220127/20119035Cut6icbfb4.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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

    public void onClick(View view) {
    }
}



JAVA 檔-照順序id

https://ithelp.ithome.com.tw/upload/images/20220127/20119035SlxplwpG4y.png

反紅就是要ALT+ENTER按import class

宣告變數:

https://ithelp.ithome.com.tw/upload/images/20220129/20119035gFqIEFhMzo.png

用字串string讓顯示的顯現出來~

String all="";

https://ithelp.ithome.com.tw/upload/images/20220129/20119035Jf5CjcfGZC.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";


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

    public void onClick(View view) {
    }
}


再來要初始化-在onCreate

用 findViewById 找出 sex

https://ithelp.ithome.com.tw/upload/images/20220129/20119035F2LeeX42FD.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);

        //初始化CheckBox按鈕


    }

    public void onClick(View view) {
    }
}

裡面管3個~
當要預設選項"預設"被V時-只能選一個

Common Attributes-checked要V
https://ithelp.ithome.com.tw/upload/images/20220129/201190355IVoxOMdEt.png

clickable要預設為true 就是可以讓"使用者"可以選

https://ithelp.ithome.com.tw/upload/images/20220129/20119035q8SyERustU.png

從程式碼來看:

https://ithelp.ithome.com.tw/upload/images/20220129/20119035afsJzVVot8.png


再回到java檔

加入偵聽器-RadioGroup有自家的要選

https://ithelp.ithome.com.tw/upload/images/20220129/20119035SV2aMAvfHt.png

選擇OnCheckChangeListener

https://ithelp.ithome.com.tw/upload/images/20220129/20119035OCZ7H7k0eb.png

原來的 int i

https://ithelp.ithome.com.tw/upload/images/20220129/20119035JFELloFIvv.png

改成 int checkedId就是指int的r1,r2,r3的哪個id被點到了

https://ithelp.ithome.com.tw/upload/images/20220129/20119035Fyt4BYvRZa.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";

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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

            }
        });



        //初始化CheckBox按鈕


    }

    public void onClick(View view) {
    }
}

用switch去寫int checkedId到底選到哪一個

https://ithelp.ithome.com.tw/upload/images/20220129/20119035YF5ps14hnO.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";

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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                }

            }
        });



        //初始化CheckBox按鈕


    }

    public void onClick(View view) {
    }
}

蒐集文字要宣告字串
RadioGroup是s1
CheckBox 是s2

https://ithelp.ithome.com.tw/upload/images/20220129/20119035a0J8b4zDvd.png


如果是r1被點到就內容"先生"顯示使用Toast模擬器顯示不出來建議使用手機
switch後面一定要有break;

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this,
                                s1,
                                Toast.LENGTH_SHORT).show();
                        break;


                }

後面依序r2和r3

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1,s2;


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });



        //初始化CheckBox按鈕


    }

    public void onClick(View view) {
    }
}

https://ithelp.ithome.com.tw/upload/images/20220129/20119035hF5dQBlcyz.jpg

https://ithelp.ithome.com.tw/upload/images/20220129/20119035QpRqgfyGA3.jpg


初始化CheckBox按鈕

https://ithelp.ithome.com.tw/upload/images/20220129/20119035Ax7SpMyHcg.png

//初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

加 加入偵聽器-比較麻煩-一個一個去看有沒有被點選

https://ithelp.ithome.com.tw/upload/images/20220129/201190352gV7ScXLIy.png

c1.setOnCheckedChangeListener(new MM());
中new MM()目前是假設

https://ithelp.ithome.com.tw/upload/images/20220129/20119035WUTXO3YXqq.png

初始化TextView

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1,s2;


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });

        //初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

        c1.setOnCheckedChangeListener(new MM());
        c2.setOnCheckedChangeListener(new MM());
        c3.setOnCheckedChangeListener(new MM());


        show = findViewById(R.id.show);


    }

    public void onClick(View view) {
    }
}

https://ithelp.ithome.com.tw/upload/images/20220129/20119035lTixgKv1kz.png


離開了 onCreate
CheckBox的偵聽器:內部類

類所以是class

使用alt+enter讓他跳出提示解決方

CheckBox的偵聽器:內部類
https://ithelp.ithome.com.tw/upload/images/20220129/20119035BVaPastNba.png

按下變這樣

https://ithelp.ithome.com.tw/upload/images/20220129/20119035H6EqXUeoob.png

再移上去

https://ithelp.ithome.com.tw/upload/images/20220129/20119035ZMhazAcaQ9.png

implements的3種顯示方法:
1-紅色燈泡implements
https://ithelp.ithome.com.tw/upload/images/20220129/20119035bIFAEPRENZ.png
2-Alt+Insert -->implements

https://ithelp.ithome.com.tw/upload/images/20220129/201190358qAhZRqdbx.png
3-右鍵-->Generate -->implements這裡選這種
https://ithelp.ithome.com.tw/upload/images/20220129/20119035oVvrceXmEf.png

https://ithelp.ithome.com.tw/upload/images/20220129/20119035i3waovAlMa.png

https://ithelp.ithome.com.tw/upload/images/20220129/201190356yEp99fkny.png

extends與implements的區別:

extends 是繼承父類,只要那個類不是宣告為final或者那個類定義為abstract的就能繼承,JAVA中不支援多重繼承,但是可以用介面來實現,這樣就用到了implements,繼承只能繼承一個類,但implements可以實現多個介面,用逗號分開就行了。

https://ithelp.ithome.com.tw/upload/images/20220129/20119035R4e17V6dTT.png

改寫 compoundButton, boolean b變成CompoundButton buttonView, boolean isChecked
https://ithelp.ithome.com.tw/upload/images/20220129/20119035KTeQQo4aLI.png

https://ithelp.ithome.com.tw/upload/images/20220129/20119035kTYm9kV2Er.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1,s2;


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });

        //初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

        c1.setOnCheckedChangeListener(new MM());
        c2.setOnCheckedChangeListener(new MM());
        c3.setOnCheckedChangeListener(new MM());

        show = findViewById(R.id.show);
            }

    //CheckBox的偵聽器:內部類

    private class MM implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        }
    }

    public void onClick(View view) {
    }


}

CheckBox的偵聽器:內部類

1.一般父類->判斷元件
2.取出元件名稱
3.再用if去判斷 +判斷點選狀態

https://ithelp.ithome.com.tw/upload/images/20220129/201190352DqR0nUWYk.png

裡面也是加入toast用手機測

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1,s2;


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });

        //初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

        c1.setOnCheckedChangeListener(new MM());
        c2.setOnCheckedChangeListener(new MM());
        c3.setOnCheckedChangeListener(new MM());

        show = findViewById(R.id.show);
            }

    //CheckBox的偵聽器:內部類

    private class MM implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //判斷元件
            CheckBox box=(CheckBox) buttonView;

            //取出元件的名稱
            String name = box.getText().toString();

            //判斷點選狀態

            if(isChecked){
                Toast.makeText(MainActivity.this,name+"被選中",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,name+"被取消",Toast.LENGTH_SHORT).show();
            }

        }
    }

    public void onClick(View view) {
    }


}

也是要用手機測不然TOAST模擬器不能
https://ithelp.ithome.com.tw/upload/images/20220129/201190351N3R8VO6bg.jpg


太長收起來
https://ithelp.ithome.com.tw/upload/images/20220129/20119035PrySXu9tJR.png


按鈕收集資料,顯示在show-常用
https://ithelp.ithome.com.tw/upload/images/20220129/201190353SYHabMMN6.png

使用for each的語法:
for-each 迴圈

當我們想要存取一個陣列或集合裡面的元素時,for-each迴圈是個簡單且有效率的方法

for-each迴圈的格式如下:

for(元素型態 迴圈控制變數 : 集合或陣列名稱)

//迴圈主體;

for-each有幾項注意要點

1.只能從頭開始走訪每個元素,不能從集合或陣列的尾端向前走訪

2.只能取出集合或陣列裡的元素不能置換

3.foe-each迴圈裡面的變數是區域變數

下面這個範例在示範傳統for迴圈和for-each迴圈讀取陣列元素

你會發現for-each簡潔許多

public class HelloDate {
    public static void main(String args[]) {
        int arr[]={5,3,8};
       
        //傳統for迴圈
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
       
        //for-each loop
        for(int i:arr){
            System.out.print(i+" ");
        }
    }
}

這裡寫的-

public void onClick(View view) {
        //收集checkBox的結果
        int[] cbs ={R.id.c1,R.id.c2,R.id.c3};
        CheckBox cc;
        for(int i:cbs){
            cc =findViewById(i);
            if(cc.isChecked()){
                s2 += cc.getText()+"\t";
            }
        }

        all = s1+"\n"+s2;
    }


目前顯示的全部:

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1,s2;


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });

        //初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

        c1.setOnCheckedChangeListener(new MM());
        c2.setOnCheckedChangeListener(new MM());
        c3.setOnCheckedChangeListener(new MM());

        show = findViewById(R.id.show);
            }

    //CheckBox的偵聽器:內部類

    private class MM implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //判斷元件
            CheckBox box=(CheckBox) buttonView;

            //取出元件的名稱
            String name = box.getText().toString();

            //判斷點選狀態

            if(isChecked){
                Toast.makeText(MainActivity.this,name+"被選中",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,name+"被取消",Toast.LENGTH_SHORT).show();
            }

        }
    }

    //按鈕收集資料,顯示在show
    public void onClick(View view) {
        //收集checkBox的結果
        int[] cbs ={R.id.c1,R.id.c2,R.id.c3};
        CheckBox cc;
        for(int i:cbs){
            cc =findViewById(i);
            if(cc.isChecked()){
                s2 += cc.getText()+"\t";
            }
        }

        all = s1+"\n"+s2;
    }


}

要顯示要寫show

https://ithelp.ithome.com.tw/upload/images/20220129/20119035M8nvhyxi74.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1,s2;


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });

        //初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

        c1.setOnCheckedChangeListener(new MM());
        c2.setOnCheckedChangeListener(new MM());
        c3.setOnCheckedChangeListener(new MM());

        show = findViewById(R.id.show);
            }

    //CheckBox的偵聽器:內部類

    private class MM implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //判斷元件
            CheckBox box=(CheckBox) buttonView;

            //取出元件的名稱
            String name = box.getText().toString();

            //判斷點選狀態

            if(isChecked){
                Toast.makeText(MainActivity.this,name+"被選中",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,name+"被取消",Toast.LENGTH_SHORT).show();
            }

        }
    }

    //按鈕收集資料,顯示在show
    public void onClick(View view) {
        //收集checkBox的結果
        int[] cbs ={R.id.c1,R.id.c2,R.id.c3};
        CheckBox cc;
        for(int i:cbs){
            cc =findViewById(i);
            if(cc.isChecked()){
                s2 += cc.getText()+"\t";
            }
        }

        all = s1+"\n"+s2;
        show.setText(all);
    }


}

https://ithelp.ithome.com.tw/upload/images/20220129/20119035WA0OMHmGqG.jpg


如果不要按鈕直接空按是空白
要改-s1
https://ithelp.ithome.com.tw/upload/images/20220130/20119035flkyEaWkwF.png

https://ithelp.ithome.com.tw/upload/images/20220130/20119035ZlADfMg47I.jpg

https://ithelp.ithome.com.tw/upload/images/20220130/20119035rjhQHFRPmA.jpg

https://ithelp.ithome.com.tw/upload/images/20220130/20119035TCr34M5MZJ.jpg
累積經驗越寫越完美


如果不要按鈕直接空按是空白
要改-s2

https://ithelp.ithome.com.tw/upload/images/20220130/20119035lCThb7YILs.png

https://ithelp.ithome.com.tw/upload/images/20220130/20119035RbHrvoXKGt.png

下面多判斷式:

https://ithelp.ithome.com.tw/upload/images/20220130/20119035EvMKjy4Av6.jpg

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1="先生",s2="沒有點選";


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });

        //初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

        //c1.setOnCheckedChangeListener(new MM());
        //c2.setOnCheckedChangeListener(new MM());
        //c3.setOnCheckedChangeListener(new MM());

        show = findViewById(R.id.show);
            }

    //CheckBox的偵聽器:內部類

    private class MM implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //判斷元件
            CheckBox box=(CheckBox) buttonView;

            //取出元件的名稱
            String name = box.getText().toString();

            //判斷點選狀態

            if(isChecked){
                Toast.makeText(MainActivity.this,name+"被選中",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,name+"被取消",Toast.LENGTH_SHORT).show();
            }

        }
    }

    //按鈕收集資料,顯示在show
    public void onClick(View view) {
        //先清空s2
        s2="";

        //收集checkBox的結果
        int[] cbs ={R.id.c1,R.id.c2,R.id.c3};
        CheckBox cc;
        for(int i:cbs){
            //CheckBox cc =findViewById(i);

            cc =findViewById(i);
            if(cc.isChecked()){
                s2 += cc.getText()+"\t";
            }
        }

        //判斷文字欄位是否為空

        if(TextUtils.isEmpty(s2)){
            s2="沒有點選";
        }

        all = s1+"\n"+s2;
        show.setText(all);
    }


}

TextUtils.isEmpty的方法。但其實TextUtils不止有這一個方法。這裡我列舉出TextUtils中常用的四個方法,供大家需要時查閱。

isEmpty:判斷字串是否為空值。
getTrimmedLength:獲取字串去除頭尾空格之後的長度。
isDigitsOnly:判斷字串是否全部由數字組成。
ellipsize:如果字串超長,則返回按規則截斷並新增省略號的字串。


延續上一篇因為太長了
目前的程式碼:

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1,s2;


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });

        //初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

        c1.setOnCheckedChangeListener(new MM());
        c2.setOnCheckedChangeListener(new MM());
        c3.setOnCheckedChangeListener(new MM());

        show = findViewById(R.id.show);
            }

    //CheckBox的偵聽器:內部類

    private class MM implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //判斷元件
            CheckBox box=(CheckBox) buttonView;

            //取出元件的名稱
            String name = box.getText().toString();

            //判斷點選狀態

            if(isChecked){
                Toast.makeText(MainActivity.this,name+"被選中",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,name+"被取消",Toast.LENGTH_SHORT).show();
            }

        }
    }

    //按鈕收集資料,顯示在show
    public void onClick(View view) {
        //收集checkBox的結果
        int[] cbs ={R.id.c1,R.id.c2,R.id.c3};
        CheckBox cc;
        for(int i:cbs){
            cc =findViewById(i);
            if(cc.isChecked()){
                s2 += cc.getText()+"\t";
            }
        }

        all = s1+"\n"+s2;
        show.setText(all);
    }


}

手機顯示:
https://ithelp.ithome.com.tw/upload/images/20220129/20119035zEeLh0Ugiy.jpg


https://ithelp.ithome.com.tw/upload/images/20220130/20119035klB3TsU9TQ.png

c1,c2,c3也可以比較簡單的寫法:

setOnCheckedChangeListener偵聽器關掉也是可以

https://ithelp.ithome.com.tw/upload/images/20220129/20119035Kr4WkVNBI1.png

下面配合上面

private class MM implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //判斷元件
            CheckBox box=(CheckBox) buttonView;

            //取出元件的名稱
            String name = box.getText().toString();

            //判斷點選狀態

            if(isChecked){
                Toast.makeText(MainActivity.this,name+"被選中",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,name+"被取消",Toast.LENGTH_SHORT).show();
            }

        }
    }

https://ithelp.ithome.com.tw/upload/images/20220130/201190351B7IGBm2Bv.png


目前會有null是因為沒有初始化完成

https://ithelp.ithome.com.tw/upload/images/20220130/20119035Y64I73D5j1.jpg

要改 String s1,s2;這句

要變成String s1="",s2="";

https://ithelp.ithome.com.tw/upload/images/20220130/20119035KUn7WXAL4N.jpg

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1="",s2="";


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });

        //初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

        //c1.setOnCheckedChangeListener(new MM());
        //c2.setOnCheckedChangeListener(new MM());
        //c3.setOnCheckedChangeListener(new MM());

        show = findViewById(R.id.show);
            }

    //CheckBox的偵聽器:內部類

    private class MM implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //判斷元件
            CheckBox box=(CheckBox) buttonView;

            //取出元件的名稱
            String name = box.getText().toString();

            //判斷點選狀態

            if(isChecked){
                Toast.makeText(MainActivity.this,name+"被選中",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,name+"被取消",Toast.LENGTH_SHORT).show();
            }

        }
    }

    //按鈕收集資料,顯示在show
    public void onClick(View view) {
        //收集checkBox的結果
        int[] cbs ={R.id.c1,R.id.c2,R.id.c3};
        CheckBox cc;
        for(int i:cbs){
            //CheckBox cc =findViewById(i);

            cc =findViewById(i);
            if(cc.isChecked()){
                s2 += cc.getText()+"\t";
            }
        }

        all = s1+"\n"+s2;
        show.setText(all);
    }


}

當使用者反覆操作.下面有累積的感覺
https://ithelp.ithome.com.tw/upload/images/20220130/20119035uhliWUyQTA.jpg

就是s2先清空再收集s2="";

https://ithelp.ithome.com.tw/upload/images/20220130/201190359NWpESuZnG.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    RadioGroup sex;
    CheckBox c1,c2,c3;
    TextView show;
    String all="";
    String s1="",s2="";


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

        //初始化RadioGroup按鈕

        sex = findViewById(R.id.sex);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                switch (checkedId){
                    case R.id.r1:
                        s1 = "先生";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r2:
                        s1 = "女士";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.r3:
                        s1 = "不顯示";
                        Toast.makeText(MainActivity.this, s1,Toast.LENGTH_SHORT).show();
                        break;


                }

            }
        });

        //初始化CheckBox按鈕
        c1 =findViewById(R.id.c1);
        c2 =findViewById(R.id.c2);
        c3 =findViewById(R.id.c3);

        //c1.setOnCheckedChangeListener(new MM());
        //c2.setOnCheckedChangeListener(new MM());
        //c3.setOnCheckedChangeListener(new MM());

        show = findViewById(R.id.show);
            }

    //CheckBox的偵聽器:內部類

    private class MM implements CompoundButton.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //判斷元件
            CheckBox box=(CheckBox) buttonView;

            //取出元件的名稱
            String name = box.getText().toString();

            //判斷點選狀態

            if(isChecked){
                Toast.makeText(MainActivity.this,name+"被選中",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this,name+"被取消",Toast.LENGTH_SHORT).show();
            }

        }
    }

    //按鈕收集資料,顯示在show
    public void onClick(View view) {
        //先清空s2
        s2="";

        //收集checkBox的結果
        int[] cbs ={R.id.c1,R.id.c2,R.id.c3};
        CheckBox cc;
        for(int i:cbs){
            //CheckBox cc =findViewById(i);

            cc =findViewById(i);
            if(cc.isChecked()){
                s2 += cc.getText()+"\t";
            }
        }

        all = s1+"\n"+s2;
        show.setText(all);
    }


}

https://ithelp.ithome.com.tw/upload/images/20220130/20119035aThtqdTudS.jpg


/images/emoticon/emoticon06.gif


一個app很少只有一個畫面-頁面跳轉
/images/emoticon/emoticon06.gif
所以後面就要難難的了-要傳DATA

來寫-成列商品-listview
跟spinner不同(這個要按展開紐)

點餐~20220130再做一次

重點:
1-新增Activity
2-版面規劃-使用參考線(用%這個真的難~>< )
3-頁面跳轉


版面規劃-使用參考線(用%這個真的難~>< )

https://ithelp.ithome.com.tw/upload/images/20220130/20119035xTrTH79HBC.png


祝大家中秋節快樂~
因為去了"和風"文化季~

當手殘把 #android 工具列關掉時???

https://matters.news/@ailefromfutu/android-studio-main-menu-%E5%B7%A5%E5%85%B7%E5%88%97%E6%B6%88%E5%A4%B1%E4%BA%86%E8%A9%B2%E6%80%8E%E9%BA%BC%E5%9B%9E%E5%BE%A9-bafyreids2bp7dhkm27xetgegdalu4kddtpih7bmgrb6yhqlumvyffkbx6a


當 #android 出現錯誤訊息

#The minCompileSdk (31) is greater than this module's compileSdkVersion (android-30)

https://www.youtube.com/watch?v=4gUzUuk9X0k


太長了~所以剩下一篇

下篇來寫-成列商品-listview
跟spinner不同(這個要按展開紐)
/images/emoticon/emoticon06.gif


上一篇
第4天~點餐系統
下一篇
第6天~原來還有OKhttp這種語法
系列文
就是從無到有寫app31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
haraguroicha
iT邦新手 5 級 ‧ 2021-09-21 15:05:11

中秋快樂,記得補文

我要留言

立即登入留言