iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 27
0
Mobile Development

Android Studio 學習交流系列 第 27

[Day27]Android學習-創新挑戰-健康管理app(5)

  • 分享至 

  • twitterImage
  •  

本篇專案內容將繼續實現血壓的紀錄功能,Let's go!
/images/emoticon/emoticon07.gif

本篇重點

  1. 設計血壓新增頁面
  2. 設計血壓新增頁面的流程控制

應具備能力

  1. Android學習-資料庫介紹-資料操縱(1)
  2. Android學習-資料庫介紹-SQLiteDataBase類別(2)
  3. Android學習-資料庫介紹-資料庫實作(3)
  4. Android學習-元件介紹-Layout
  5. Android學習-元件介紹-Text系列(2)

製作專案

實作新增頁面,提供UI讓使用者輸入血壓、脈搏等數值

pressure_create.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="血壓新增頁面"
            android:gravity="center_horizontal"
            android:textSize="30dp"/>

    </FrameLayout>


    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edtHigh"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:hint="收縮壓mm/Hg"
            android:textSize="25dp"/>
    </com.google.android.material.textfield.TextInputLayout>
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edtLow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:hint="舒張壓mm/Hg"
            android:textSize="25dp"/>
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edtBump"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:hint="脈搏 min/次"
            android:textSize="25dp"/>
    </com.google.android.material.textfield.TextInputLayout>
    <Button
        android:id="@+id/btnAppend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="新增"
        />
</LinearLayout>
  • 提供使用者輸入血壓、脈搏等數值
  • TextInputLayout 可以在使用者輸入時,將提示往上顯示,與一般的EditText相比,使用者輸入會看不到提示,相關內容參見 Android學習-元件介紹-Text系列(2)

PressureCreateActivity.java

        btnAppend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {        db.append(num_chg_ten(month+1)+num_chg_ten(date),Integer.parseInt(edtHigh.getText().toString()),Integer.parseInt(edtLow.getText().toString()));
                finish();
            }
        });
    }
  • btnAppend.setOnClickListener 偵聽是否觸發新增按鈕
  • db.append 使用HealthDB類別的方法,將資料新增至資料庫
  • finish() 新增後,結束此頁面
    private void gettime() {
        Calendar calendar=Calendar.getInstance();
        month = calendar.get(Calendar.MONTH);
        date = calendar.get(Calendar.DATE);
        int hour=calendar.get(Calendar.HOUR);
        int min=calendar.get(Calendar.MINUTE);
        txtTime.setText(month +1+"/"+ date +"  "+num_chg_ten(hour)+":"+num_chg_ten(min));
    }
  • Calendar.getInstance() 得到Calendar物件
  • calendar.get() 透過指定的參數獲得系統當下的時間
    public String num_chg_ten(int num){
        String text = null;
        if(num<10){
            text="0"+num;
        }
        else{
            text=String.valueOf(num);
        }
        return text;
    }
  • num_chg_ten()此方法使尚未有十位數的數字,前面加入"0"的符號,例如:2 => 02

完整程式碼

public class PressureCreateActivity extends AppCompatActivity {
    private HealthDB db =new HealthDB(PressureCreateActivity.this);
    private TextInputEditText edtHigh,edtLow,edtBump;
    private Button btnAppend;
    private int month;
    private int date;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pressure_create);
        findViews();
        //  獲得目前時間
        gettime();
    }

    private void findViews() {
    
        edtHigh=(TextInputEditText)findViewById(R.id.edtHigh);
        edtLow=(TextInputEditText)findViewById(R.id.edtLow);
        edtBump=(TextInputEditText)findViewById(R.id.edtBump);
        btnAppend=(Button)findViewById(R.id.btnAppend);
        btnAppend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                db.append(num_chg_ten(month+1)+"/"+num_chg_ten(date),
                        Integer.parseInt(edtHigh.getText().toString()),
                        Integer.parseInt(edtLow.getText().toString()),
                        Integer.parseInt(edtBump.getText().toString()));
                finish();
            }
        });
    }

    private void gettime() {
    
        Calendar calendar=Calendar.getInstance();
        month = calendar.get(Calendar.MONTH);
        date = calendar.get(Calendar.DATE);
        int hour=calendar.get(Calendar.HOUR);
        int min=calendar.get(Calendar.MINUTE);
        //   標題上顯示時間
        setTitle(getTitle().toString()+"-"
        +(month +1)+"/"+ date +"  "+num_chg_ten(hour)+":"+num_chg_ten(min));
    }

    public String num_chg_ten(int num){
        String text = null;
        //   個位數字前面增加"0"
        if(num<10){
            text="0"+num;
        }
        else{
            text=String.valueOf(num);
        }
        return text;
    }
}

成果

恭喜大家正式將血壓功能完成囉!現在我們來進行功能測試吧!

進入新增畫面
https://ithelp.ithome.com.tw/upload/images/20191011/20121149U3NDzoj7IY.png

輸入測量數值
https://ithelp.ithome.com.tw/upload/images/20191011/201211492PVjNbhU7f.png

完成輸入,進入血壓管理頁面
https://ithelp.ithome.com.tw/upload/images/20191011/20121149KdH1hSKgHT.png

長壓選單按鈕,詢問是否刪除資料
https://ithelp.ithome.com.tw/upload/images/20191011/20121149WUrJmPL0Oq.png

確認刪除血壓紀錄
https://ithelp.ithome.com.tw/upload/images/20191011/201211495rAnaOFRtb.png

若文章有誤,歡迎大家提出建議。

Thank you for your time.

/images/emoticon/emoticon37.gif/images/emoticon/emoticon61.gif


上一篇
[Day26]Android學習-創新挑戰-健康管理app(4)
下一篇
[Day28]Android學習-創新挑戰-健康管理app(6)
系列文
Android Studio 學習交流30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
enochwu
iT邦新手 5 級 ‧ 2020-05-12 14:45:51

不好意思
請問如何寫血糖的新增頁面

讀者你好,請參考這兩篇文章[Day26]Android學習-創新挑戰-健康管理app(4)[Day27]Android學習-創新挑戰-健康管理app(5)做改寫即可完成血糖的新增頁面囉!

我要留言

立即登入留言