第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


<<:  Day#05 Storyboard

>>:  [DAY 05]物品拍卖价格查询功能(3/4)

Day-22 树(Tree), 二元搜寻树(Binary Search Tree)

前言 对於大量的资料处理,使用串列的走访是一种十分没有效率的方法,其效率会根据串列的长度而不断线性成...

Day 07 Style

第七天~ 我们昨天写了个 Hello World 出来, 但是这只是单单把字给秀出来而已, 假如我们...

[D17] ML机器学习(入门)

之前有提到 CNN 业会用在 ML ,那甚麽是 ML 呢? Machine Learning 机器学...

[Day17] 藉由DialogFlow Fulfillment 实践对话流设计

藉由Google Cloud Function建构DialogFlow Fulfillment 透过...

Day18 Combine 05 - Operators 类型介绍 : 转换操作符

转换操作符 map/mapError map操作符会执行给定的闭包,将上游发布的内容进行转换,然後再...