iT邦幫忙

2021 iThome 鐵人賽

DAY 4
0
Mobile Development

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

第4天~點餐系統

  • 分享至 

  • xImage
  •  

2022/1/19再練習一次:

改最上面標題的地方:

https://ithelp.ithome.com.tw/upload/images/20220123/20119035QxdfuRVdQ8.png

使用按鈕ToggleButton和Switch

文字選large-文字large

https://ithelp.ithome.com.tw/upload/images/20220119/20119035Zfv1RC5jar.png


Plain Text-
設定id不能寫中文
https://ithelp.ithome.com.tw/upload/images/20220119/20119035eQQrNhUjjn.png


搜尋toggle button-2選1

https://ithelp.ithome.com.tw/upload/images/20220119/20119035vf6hPSTz4x.png

https://ithelp.ithome.com.tw/upload/images/20220119/20119035PLUdgNtBVp.png

設定id
https://ithelp.ithome.com.tw/upload/images/20220119/20119035iZ2Sui9WOf.png


switch1-2選1
https://ithelp.ithome.com.tw/upload/images/20220119/20119035hB35GKgFKF.png

編輯文字內容:switch1-1
https://ithelp.ithome.com.tw/upload/images/20220119/20119035LFDExIdEAl.png


再來是button
https://ithelp.ithome.com.tw/upload/images/20220119/201190353UHyFkusTD.png

用onClick就不用借助id不用綁定
button用onClick綁定

https://ithelp.ithome.com.tw/upload/images/20220120/20119035BnWCj0E89r.png

button用onClick綁定-因為還沒有綁定所以才反紅-因為java檔還沒有動

https://ithelp.ithome.com.tw/upload/images/20220120/20119035pxjseohzWr.png

button用onClick綁定-因為還沒有綁定所以才反紅-用燈泡來建立
https://ithelp.ithome.com.tw/upload/images/20220120/20119035DszuwnM03p.png

button用onClick綁定-因為還沒有綁定所以才反紅-建立onClick

https://ithelp.ithome.com.tw/upload/images/20220120/20119035ScE5rPOfjc.png

https://ithelp.ithome.com.tw/upload/images/20220120/201190355XdCqiuezW.png

裡面的參數是固定的

public void onClick(View view) {
    }

整個Code

package com.huang.myui2;

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) {
    }
}

https://ithelp.ithome.com.tw/upload/images/20220120/20119035jgA4ArxWHV.png


各部分按鍵id和onClick

https://ithelp.ithome.com.tw/upload/images/20220120/20119035Gg1ekRCktx.png


安卓-流程:

1-宣告變數
2-初始化變數
3-各自賦予功能


各部分按鍵id和onClick

https://ithelp.ithome.com.tw/upload/images/20220120/20119035Gg1ekRCktx.png

變數跟id一樣比較好記(沒有一定要一樣)
安卓-流程:

1-宣告變數
2-初始化變數
3-各自賦予功能


package com.huang.myui2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;


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

    public void onClick(View view) {
    }
}

https://ithelp.ithome.com.tw/upload/images/20220120/201190353i2rALHLkF.png

onCreate 是 版面/元件 初始化-功能:

1- java結合 xml
2- 賦予功能


變數跟id一樣比較好記(沒有一定要一樣)
安卓-流程:

1-宣告變數
2-初始化變數
3-賦予功能

R的意思是目錄-裡面有 id / layout /string ....

先右findViewById(R.id.input);

後左input

https://ithelp.ithome.com.tw/upload/images/20220120/20119035YqOrMq1uCK.png

package com.huang.myui2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;


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

        input = findViewById(R.id.input);
        

    }

    public void onClick(View view) {
    }
}

初始化變數-

https://ithelp.ithome.com.tw/upload/images/20220120/201190358x9RxUtj7M.png

package com.huang.myui2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;


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

        input = findViewById(R.id.input);
        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);


    }

    public void onClick(View view) {
    }
}

繼續初始化onCreate-宣告變數

https://ithelp.ithome.com.tw/upload/images/20220123/20119035s63NDXdgEi.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;

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

    public void onClick(View view) {
    }
}




寫在onCreat

1-綁定=java結合xml
2-賦予功能


3-各自賦予功能-R檔是APP的目錄檔

先右後左
input = findViewById(R.id.input);

https://ithelp.ithome.com.tw/upload/images/20220123/20119035UNuVvYuXau.png


繼續初始化- //初始化元件

https://ithelp.ithome.com.tw/upload/images/20220123/20119035wdZwkhCMuH.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;

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

        input = findViewById(R.id.input);
        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);

    }

    public void onClick(View view) {
    }
}


賦予功能:

EditText input; 接收資料
ToggleButton toggleButton;賦予功能-裝偵聽器

![https://ithelp.ithome.com.tw/upload/images/20220123/20119035P6lBUQEI47.png](https://ithelp.ithome.com.tw/upload/images/20220123/20119035P6lBUQEI47.png)

產生物件:
![https://ithelp.ithome.com.tw/upload/images/20220123/20119035pnZarGv2V6.png](https://ithelp.ithome.com.tw/upload/images/20220123/20119035pnZarGv2V6.png)

手動改

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)

目前程式碼:

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;

    //初始化元件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);

        //賦予功能:

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });

    }

    public void onClick(View view) {
    }
}

使用Toast就是在APP上短暫停留然後消失


使用Toast就是在APP上短暫停留然後消失

來java寫if和else

這裡的true是
if(isChecked)

https://ithelp.ithome.com.tw/upload/images/20220123/20119035naB9gwTvU2.png

makeText()
裡面要放3樣才不會反紅;

1- MainActivity.this-MainActivity.this
2-要開還是關或任何文字-"OK"
3-顯示時間-LENGTH_SHORT 顯示短 /

https://ithelp.ithome.com.tw/upload/images/20220123/20119035v0E42O4aLQ.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;

    //初始化元件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);

        //賦予功能:

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_SHORT).show();
                }
                else {}

            }
        });

    }

    public void onClick(View view) {
    }
}

https://ithelp.ithome.com.tw/upload/images/20220123/201190357VqJwE3iwE.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;

    //初始化元件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);

        //賦予功能:

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_SHORT).show();
                }
                else {Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_SHORT).show();

                }

            }
        });

    }

    public void onClick(View view) {
    }
}

這裡我的Toast應該是因為記憶體不足顯示不出來

https://ithelp.ithome.com.tw/upload/images/20220123/201190358h0NjGDIkF.png

在手機上可以

https://ithelp.ithome.com.tw/upload/images/20220123/20119035d0zl5o6B18.jpg


//switch1賦予功能:
https://ithelp.ithome.com.tw/upload/images/20220123/20119035uuZqnibdmT.png


Switch switch1;賦予功能

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;

    //初始化元件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);

        //toggleButton賦予功能:

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                }
                else {Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();

                }

            }
        });
        //switch1賦予功能:

        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                }
                else {Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();

                }

            }
        });

    }

    public void onClick(View view) {
    }
}

也是用手機測試:
https://ithelp.ithome.com.tw/upload/images/20220123/20119035cUhjixbkq8.jpg


//透過button蒐集資料
https://ithelp.ithome.com.tw/upload/images/20220123/20119035FUFE7kDiQu.png

要先宣告空的字串:
String str = "";

button宣告的字串-

String str1,str2,str3;

str1是togglebutton 變數

str2是switch 變數

str3是edittext 變數

按鈕按下顯示-

https://ithelp.ithome.com.tw/upload/images/20220123/20119035e0P69EJBGR.png

然後再回到程式碼裡去寫:

從togglebutton
https://ithelp.ithome.com.tw/upload/images/20220123/20119035NYOvxE0UZD.png

從switch
https://ithelp.ithome.com.tw/upload/images/20220123/20119035UcfhaqTu2q.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;
    String str1,str2,str3;

    //初始化元件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);

        //toggleButton賦予功能:

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                    str1 = "toggleButton-->OK";
                }
                else {
                    Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();
                    str1 = "toggleButton-->NO";
                }

            }
        });
        //switch1賦予功能:

        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                    str2 = "switch1-->OK";
                }
                else {Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();
                    str2 = "switch1-->NO";

                }

            }
        });

    }

    //透過button蒐集資料

    public void onClick(View view) {
        
    }
}

從edittext是寫在最下面的

public void onClick(View view) {

    }

寫裡面:
https://ithelp.ithome.com.tw/upload/images/20220123/201190359mrOJ5GQAv.png

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;
    String str1,str2,str3;

    //初始化元件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);

        //toggleButton賦予功能:

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                    str1 = "toggleButton-->OK";
                }
                else {
                    Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();
                    str1 = "toggleButton-->NO";
                }

            }
        });
        //switch1賦予功能:

        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                    str2 = "switch1-->OK";
                }
                else {Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();
                    str2 = "switch1-->NO";

                }

            }
        });

    }

    //透過button蒐集資料

    public void onClick(View view) {

        str3 = input.getText().toString();
        Toast.makeText(MainActivity.this,
                str1+"\n"+str2+"\n"+str3,
                Toast.LENGTH_LONG).show();

    }
}

https://ithelp.ithome.com.tw/upload/images/20220123/20119035pbGELmaf4I.jpg


設定畫面初始值-一開始UI看到的樣子:

從onCreate以下開始~

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

input初始值

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


toggleButton初始值-一開始UI看到的樣子:

str1 = "toggleButton-->NO";

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


switch1初始值-一開始UI看到的樣子:
https://ithelp.ithome.com.tw/upload/images/20220125/20119035iksxl8Gu7L.png


String str1,str2,str3;

str1是togglebutton 變數

str2是switch 變數

str3是edittext 變數


寫在外頭才是一開始看到的樣子~

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;
    String str1,str2,str3;

    //初始化元件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        input.setText("NO INPUT");
        str1 = "toggleButton-->NO";
        str2 = "switch1-->OK";


        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);

        //toggleButton賦予功能:

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                    str1 = "toggleButton-->OK";
                }
                else {
                    Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();
                    str1 = "toggleButton-->NO";
                }

            }
        });
        //switch1賦予功能:

        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                    str2 = "switch1-->OK";
                }
                else {Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();
                    str2 = "switch1-->NO";

                }

            }
        });

    }

    //透過button蒐集資料

    public void onClick(View view) {

        str3 = input.getText().toString();
        Toast.makeText(MainActivity.this,
                str1+"\n"+str2+"\n"+str3,
                Toast.LENGTH_LONG).show();

    }
}


然後對應到最後的顯示:
https://ithelp.ithome.com.tw/upload/images/20220125/20119035MBg3gf9b2H.png

https://ithelp.ithome.com.tw/upload/images/20220125/20119035w1huNyVnPy.jpg

加入

str3 = input.getText().toString();

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

package com.huang.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    //宣告變數
    EditText input;
    ToggleButton toggleButton;
    Switch switch1;
    String str1,str2,str3;

    //初始化元件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        input.setText("NO INPUT");
        str1 = "toggleButton-->NO";
        str2 = "switch1-->NO";
        str3 = input.getText().toString();


        toggleButton = findViewById(R.id.toggleButton);
        switch1 = findViewById(R.id.switch1);

        //toggleButton賦予功能:

        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                    str1 = "toggleButton-->OK";
                }
                else {
                    Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();
                    str1 = "toggleButton-->NO";
                }

            }
        });
        //switch1賦予功能:

        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_LONG).show();
                    str2 = "switch1-->OK";
                }
                else {Toast.makeText(MainActivity.this,"NO",Toast.LENGTH_LONG).show();
                    str2 = "switch1-->NO";

                }

            }
        });

    }

    //透過button蒐集資料

    public void onClick(View view) {

        str3 = input.getText().toString();
        Toast.makeText(MainActivity.this,
                str1+"\n"+str2+"\n"+str3,
                Toast.LENGTH_LONG).show();

    }
}

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


顯示在手機上-
https://ithelp.ithome.com.tw/upload/images/20220125/20119035XosbXCmpWF.jpg

打字入-

https://ithelp.ithome.com.tw/upload/images/20220125/201190350ih7zPghw4.jpg

https://ithelp.ithome.com.tw/upload/images/20220125/20119035QMddlaEDSp.jpg



/images/emoticon/emoticon06.gif


0.0中秋連假開始~本來想好好寫~
可是跑去做和果子了~

這篇寫點餐系統-
首先先完成表格的參考線-使用guideline

https://ithelp.ithome.com.tw/upload/images/20211115/20119035r1pOXLdJYZ.png

XML排版出來的程式碼:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--  繪製參考線  -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.1" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.9" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.3" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.18" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.25" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.35" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.6" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.7" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.8" />

    <!-- 元件標題 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="好吃便當訂購表"
        android:textColor="@color/design_default_color_error"
        android:textSize="36sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline4"
        app:layout_constraintEnd_toStartOf="@+id/guideline0"
        app:layout_constraintStart_toStartOf="@+id/guideline1"
        app:layout_constraintTop_toTopOf="@+id/guideline3" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="電話"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@id/guideline5"
        app:layout_constraintEnd_toStartOf="@+id/guideline0"
        app:layout_constraintStart_toStartOf="@+id/guideline1"
        app:layout_constraintTop_toTopOf="@id/guideline4" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="主餐"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@id/guideline6"
        app:layout_constraintEnd_toStartOf="@+id/guideline0"
        app:layout_constraintStart_toStartOf="@+id/guideline1"
        app:layout_constraintTop_toTopOf="@id/guideline5" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="飲料"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@id/guideline7"
        app:layout_constraintEnd_toStartOf="@+id/guideline0"
        app:layout_constraintStart_toStartOf="@+id/guideline1"
        app:layout_constraintTop_toTopOf="@id/guideline6" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="備註"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@id/guideline8"
        app:layout_constraintEnd_toStartOf="@+id/guideline0"
        app:layout_constraintStart_toStartOf="@+id/guideline1"
        app:layout_constraintTop_toTopOf="@id/guideline7" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="數量"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@id/guideline9"
        app:layout_constraintEnd_toStartOf="@+id/guideline0"
        app:layout_constraintStart_toStartOf="@+id/guideline1"
        app:layout_constraintTop_toTopOf="@id/guideline8" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小計"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline10"
        app:layout_constraintEnd_toStartOf="@+id/guideline0"
        app:layout_constraintStart_toStartOf="@+id/guideline1"
        app:layout_constraintTop_toTopOf="@id/guideline9" />




</androidx.constraintlayout.widget.ConstraintLayout>

貼的地方:
https://ithelp.ithome.com.tw/upload/images/20211115/20119035hM0y5YvRm2.png

design是長這樣~
https://ithelp.ithome.com.tw/upload/images/20211115/20119035bAqBqujN9M.png

使用textview去放入文字

https://ithelp.ithome.com.tw/upload/images/20211115/20119035YowTCBHQmn.png

跟表格內的線對應拉~用模擬器看是長這樣
https://ithelp.ithome.com.tw/upload/images/20211115/20119035A5Bn5rCe7Z.png

都是依照參考線去改
https://ithelp.ithome.com.tw/upload/images/20211115/20119035w6UsxhKVW9.png

準備開始輸入:
https://ithelp.ithome.com.tw/upload/images/20211115/20119035SCbVO6KGWD.png


上一篇
第3天~調查表
下一篇
第5天~
系列文
就是從無到有寫app31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
haraguroicha
iT邦新手 5 級 ‧ 2021-09-21 15:07:55

避免我忘記留言,先留了之後再追加評論
好的和菓子

Tzu iT邦研究生 5 級 ‧ 2021-09-22 06:20:44 檢舉

我要留言

立即登入留言