【BMI指数计算器V2.0】项目实战
生活随笔
收集整理的這篇文章主要介紹了
【BMI指数计算器V2.0】项目实战
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
【BMI指數(shù)計算器V2.0】是建立在【BMI指數(shù)計算器V1.0】的基礎(chǔ)上進(jìn)行功能的增加,以版本迭代的方式循序漸進(jìn)的進(jìn)行Android核心技術(shù)的練習(xí)。
更新列表:
1.BMI計算標(biāo)準(zhǔn)選擇
2.體重狀態(tài)表情
3.輸入框數(shù)據(jù)范圍限制與判斷
4.按鈕點(diǎn)擊效果,輸入框背景效果
5.歡迎界面
項(xiàng)目效果圖
靜態(tài)效果圖:
動態(tài)效果圖:
項(xiàng)目結(jié)構(gòu)
注意:此項(xiàng)目是在依賴appcompat_v7_9項(xiàng)目下創(chuàng)建的,需要導(dǎo)入appcompat_v7_9并依賴此項(xiàng)目。
圖片資源
界面開發(fā)
注冊文件:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.kedi.bmi"android:versionCode="2"android:versionName="2.0" ><!-- 配置SDK的最小版本號為14,最大版本號為19 --><uses-sdk android:minSdkVersion="14"android:targetSdkVersion="19" /><application android:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><!-- 注冊主界面Activity --><activity android:name=".MainActivity" ></activity><!-- 注冊歡迎界面Activity --><activity android:name=".WelcomeActivity" ><!-- 配置action,category,使得WelcomeActivity成為第一啟動界面 --><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>字符串資源文件:values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources><!-- 字符串資源文件 --><string name="app_name">BMI指數(shù)計算器</string><string name="hello_world">Hello world!</string><string name="action_settings">Settings</string><string name="title">BMI指數(shù)計算器</string><string name="height">您的身高</string><string name="cm">(厘米)cm</string><string name="weight">您的體重</string><string name="kg">(千克)kg </string><string name="cala">計算</string><string name="clear">清除</string><string name="weight_bmi">您的體重指數(shù):</string><string name="weight_state">您的體重狀況:</string><string name="tip1">身高不能為空</string><string name="tip2">輸入格式不正確</string><string name="tip3">體重不能為空</string><string name="standard_who">WHO標(biāo)準(zhǔn)</string><string name="standard_area">亞洲標(biāo)準(zhǔn)</string><string name="height_round">身高范圍100~200</string><string name="weight_round">體重范圍30~150</string></resources>尺寸資源文件:values/dimens.xml
<resources><!-- 尺寸資源文件 --><!-- Default screen margins, per the Android Design guidelines. --><dimen name="activity_horizontal_margin">16dp</dimen><dimen name="activity_vertical_margin">16dp</dimen><dimen name="text_22">22sp</dimen><dimen name="text_16">16sp</dimen><dimen name="text_18">18sp</dimen><dimen name="margin_40">40dp</dimen><dimen name="margin_16">16dp</dimen><dimen name="margin_14">14dp</dimen><dimen name="padding_16">16dp</dimen><dimen name="w_50">50dp</dimen><dimen name="h_25">25dp</dimen> </resources>顏色資源文件:values/colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"><!-- 顏色資源文件 --><color name="white" >#ffffff</color></resources>歡迎界面布局文件:layout/activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/welcome" ></RelativeLayout>主界面布局文件:layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/main"android:orientation="vertical" ><!-- 標(biāo)題欄 --><RelativeLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/title_bg" ><!-- 標(biāo)題文本控件 --><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="@string/title"android:textColor="@color/white"android:textSize="@dimen/text_22"android:textStyle="bold" /></RelativeLayout><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/margin_40"android:orientation="vertical" ><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="vertical" ><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="horizontal" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/height"android:textColor="@color/white"android:textSize="@dimen/text_16" /><EditText android:id="@+id/et_height"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/input_bg"android:gravity="center_horizontal"android:inputType="numberDecimal"android:singleLine="true"android:textSize="@dimen/text_16" ></EditText><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/cm"android:textColor="@color/white"android:textSize="@dimen/text_16" /></LinearLayout><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:baselineAligned="true"android:gravity="center_horizontal"android:orientation="horizontal" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/weight"android:textColor="@color/white"android:textSize="@dimen/text_16" /><EditText android:id="@+id/et_weight"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/input_bg"android:gravity="center_horizontal"android:inputType="numberDecimal"android:singleLine="true"android:textSize="@dimen/text_16" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/kg"android:textColor="@color/white"android:textSize="@dimen/text_16" /></LinearLayout></LinearLayout><!-- 標(biāo)準(zhǔn)可選組布局 --><RadioGroup android:id="@+id/rg_standard"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="@dimen/margin_14"android:orientation="horizontal" ><!-- WHO標(biāo)準(zhǔn)單選按鈕 --><RadioButton android:id="@+id/rb_who"android:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/rb_standard_bg"android:checked="true"android:text="@string/standard_who"android:textColor="@color/white"android:textSize="@dimen/margin_14" ></RadioButton><!-- 亞洲標(biāo)準(zhǔn)單選按鈕 --><RadioButton android:id="@+id/rb_area"android:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/rb_standard_bg"android:text="@string/standard_area"android:textColor="@color/white"android:textSize="@dimen/margin_14" ></RadioButton></RadioGroup><Button android:id="@+id/btn_cala"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="@dimen/margin_16"android:background="@drawable/btn_cala_bg"android:text="@string/cala"android:textColor="@color/white"android:textSize="@dimen/text_18" /><LinearLayout android:id="@+id/ll_result"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/margin_16"android:gravity="center_horizontal"android:orientation="vertical" ><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="horizontal" ><ImageView android:id="@+id/iv_state"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/r1" /><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/info"android:padding="@dimen/padding_16"android:layout_marginLeft="@dimen/margin_14"android:orientation="vertical" ><TextView android:id="@+id/tv_bmi"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/weight_bmi"android:textColor="@color/white"android:textSize="@dimen/text_16" /><TextView android:id="@+id/tv_state"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/weight_state"android:textColor="@color/white"android:layout_marginTop="@dimen/margin_14"android:textSize="@dimen/text_16" /></LinearLayout></LinearLayout><Button android:id="@+id/btn_clear"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginTop="@dimen/margin_16"android:background="@drawable/btn_cala_bg"android:text="@string/clear"android:textColor="@color/white"android:textSize="@dimen/text_18" /></LinearLayout></LinearLayout></LinearLayout>功能開發(fā)
歡迎界面:WelcomeActivity.java
package com.kedi.bmi;import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.Window;/*** 歡迎姐界面類* * @author 科弟* */ public class WelcomeActivity extends Activity {// 控制界面顯示2秒執(zhí)行界面挑轉(zhuǎn)的類private Handler mHandler = new Handler();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//去掉系統(tǒng)標(biāo)題欄requestWindowFeature(Window.FEATURE_NO_TITLE);//關(guān)聯(lián)XML界面setContentView(R.layout.activity_welcome);//postDelayed()方法的作用:2000毫秒后執(zhí)行某操作mHandler.postDelayed(new Runnable() {@Overridepublic void run() {//意圖類,指定界面跳轉(zhuǎn)的源界面與目標(biāo)界面Intent intent = new Intent(WelcomeActivity.this,MainActivity.class);//執(zhí)行跳轉(zhuǎn)startActivity(intent);//關(guān)閉歡迎界面finish();}}, 2000);} }主界面:MainActivity.java
package com.kedi.bmi;import java.text.DecimalFormat;import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; import android.widget.Toast;/*** BMI指數(shù)計算器主界面管理類* * @author 科弟* */ @SuppressLint("ShowToast") public class MainActivity extends Activity implements OnClickListener,OnCheckedChangeListener {private EditText mHeightInputEt;// 身高輸入框private EditText mWeightInputEt;// 體重輸入框private Button mCalaBtn;// 計算按鈕private LinearLayout mResultLl;// 計算結(jié)果布局private TextView mWeightBmiTv;// 體重指數(shù)文本private TextView mWeightStateTv;// 體重狀態(tài)文本private Button mClearBtn;// 清除按鈕private double height;// 身高private double weight;// 體重private double bmi;// bmi指數(shù)值private String state;// 體重狀態(tài)private String weightBmi;// 您的體重指數(shù):private String weightState;// 您的體重狀況:private ImageView mStateIv;// 體重狀態(tài)圖標(biāo)// 可取體重狀態(tài)值數(shù)組private String[] states = { "偏瘦", "正常", "偏胖", "肥胖", "重度肥胖", "極重度肥胖" };private int[] state_imageIds = { R.drawable.r1, R.drawable.r2,R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6 };// 標(biāo)準(zhǔn)相關(guān)布局或控件private RadioGroup mStandardRg;// 標(biāo)準(zhǔn)可選組布局private RadioButton mWhoStandardRb;// WHO標(biāo)準(zhǔn)單選按鈕private RadioButton mAreaStandardRb;// 亞洲標(biāo)準(zhǔn)單選按鈕private static final int WHO_STANDARD = 0x1;// WHO標(biāo)準(zhǔn)private static final int AREA_STANDARD = 0x2;// 亞洲標(biāo)準(zhǔn)private int mCurrentStandard = WHO_STANDARD;// 當(dāng)前標(biāo)準(zhǔn)private SharedPreferences sp;// 保存小量數(shù)據(jù)的類,數(shù)據(jù)會保存到指定文件名的XML文件中private static final String FILE_NAME = "data.xml";// 文件名@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 去掉系統(tǒng)標(biāo)題欄requestWindowFeature(Window.FEATURE_NO_TITLE);// 關(guān)聯(lián)布局文件setContentView(R.layout.activity_main);// 初始化布局或控件initView();// 注冊控件點(diǎn)擊事件setViewListener();weightBmi = getResources().getString(R.string.weight_bmi);// 您的體重指數(shù):weightState = getResources().getString(R.string.weight_state);// 您的體重狀況:}/*** 初始化布局或控件的方法*/private void initView() {mHeightInputEt = (EditText) this.findViewById(R.id.et_height);// 身高輸入框mWeightInputEt = (EditText) this.findViewById(R.id.et_weight);// 體重輸入框mCalaBtn = (Button) this.findViewById(R.id.btn_cala);// 計算按鈕mResultLl = (LinearLayout) this.findViewById(R.id.ll_result);// 計算結(jié)果布局mWeightBmiTv = (TextView) this.findViewById(R.id.tv_bmi);// 體重指數(shù)文本mWeightStateTv = (TextView) this.findViewById(R.id.tv_state);// 體重狀態(tài)文本mStateIv = (ImageView) this.findViewById(R.id.iv_state);mClearBtn = (Button) this.findViewById(R.id.btn_clear);// 清除按鈕// 默認(rèn)顯示計算按鈕(VISIBLE),隱藏結(jié)果布局(GONE)mCalaBtn.setVisibility(View.VISIBLE);mResultLl.setVisibility(View.GONE);mStandardRg = (RadioGroup) this.findViewById(R.id.rg_standard);// 標(biāo)準(zhǔn)可選組布局mWhoStandardRb = (RadioButton) this.findViewById(R.id.rb_who);// WHO標(biāo)準(zhǔn)單選按鈕mAreaStandardRb = (RadioButton) this.findViewById(R.id.rb_area);// 亞洲標(biāo)準(zhǔn)單選按鈕}/*** 注冊控件點(diǎn)擊事件的方法*/private void setViewListener() {// 注冊點(diǎn)擊事件mCalaBtn.setOnClickListener(this);mClearBtn.setOnClickListener(this);// 注冊選擇事件mStandardRg.setOnCheckedChangeListener(this);}@Overridepublic void onClick(View v) {int id = v.getId();switch (id) {case R.id.btn_cala:// 計算邏輯// 獲取身高輸入框數(shù)據(jù)String heightStr = mHeightInputEt.getText().toString().trim();// 判斷身高輸入框數(shù)據(jù)是否為空if ("".equals(heightStr) || heightStr.length() == 0) {Toast.makeText(this, getResources().getString(R.string.tip1), 0).show();} else {try {// 將String類型轉(zhuǎn)化成Double類型height = Double.valueOf(heightStr);// 對身高數(shù)據(jù)范圍進(jìn)行判斷(100.0~200.0)if (height < 100.0 || height > 200.0) {Toast.makeText(this,getResources().getString(R.string.height_round),Toast.LENGTH_SHORT).show();} else {String weightStr = mWeightInputEt.getText().toString().trim();// 體重數(shù)據(jù)非空判斷if ("".equals(weightStr) || weightStr.length() == 0) {Toast.makeText(this,getResources().getString(R.string.tip3),Toast.LENGTH_SHORT).show();} else {try {weight = Double.valueOf(weightStr);// 體重數(shù)據(jù)范圍判斷(30.0~150.0)if (weight < 30.0 || weight > 150.0) {Toast.makeText(this,getResources().getString(R.string.weight_round),Toast.LENGTH_SHORT).show();} else {// 計算bmi值calaBmi(mCurrentStandard);// 隱藏計算按鈕(GONE),顯示結(jié)果布局(VISIBLE)setViewVisible(false);}} catch (Exception e) {Toast.makeText(this,getResources().getString(R.string.tip2),Toast.LENGTH_SHORT).show();}}}} catch (Exception e) {Toast.makeText(this,getResources().getString(R.string.tip2),Toast.LENGTH_SHORT).show();}}break;case R.id.btn_clear:// 清除setViewVisible(true);break;}}/*** 計算bmi值*/private void calaBmi(int standard) {// height/100.0 cm換算成mheight = height / 100.0;bmi = weight / (height * height);if (standard == WHO_STANDARD) {if (bmi < 18.5) {state = states[0];mStateIv.setImageResource(state_imageIds[0]);} else if (bmi >= 18.5 && bmi <= 24.9) {state = states[1];mStateIv.setImageResource(state_imageIds[1]);} else if (bmi > 24.9 && bmi <= 29.9) {state = states[2];mStateIv.setImageResource(state_imageIds[2]);} else if (bmi > 29.9 && bmi <= 34.9) {state = states[3];mStateIv.setImageResource(state_imageIds[3]);} else if (bmi > 34.9 && bmi <= 39.9) {state = states[4];mStateIv.setImageResource(state_imageIds[4]);} else {state = states[5];mStateIv.setImageResource(state_imageIds[5]);}} else {if (bmi < 18.5) {state = states[0];mStateIv.setImageResource(state_imageIds[0]);} else if (bmi >= 18.5 && bmi <= 22.9) {state = states[1];mStateIv.setImageResource(state_imageIds[1]);} else if (bmi > 22.9 && bmi <= 24.9) {state = states[2];mStateIv.setImageResource(state_imageIds[2]);} else if (bmi > 24.9 && bmi <= 29.9) {state = states[3];mStateIv.setImageResource(state_imageIds[3]);} else if (bmi > 29.9 && bmi <= 39.9) {state = states[4];mStateIv.setImageResource(state_imageIds[4]);} else {state = states[5];mStateIv.setImageResource(state_imageIds[5]);}}}/*** 控制計算按鈕與結(jié)果布局的顯示與隱藏* * @param visible*/private void setViewVisible(boolean visible) {if (visible) {mCalaBtn.setVisibility(View.VISIBLE);mResultLl.setVisibility(View.GONE);// 清空數(shù)據(jù)mWeightBmiTv.setText("");mWeightStateTv.setText("");mHeightInputEt.setText("");mWeightInputEt.setText("");height = 0.0;weight = 0.0;} else {// 獲得焦點(diǎn)mHeightInputEt.requestFocus();// 格式化數(shù)據(jù)的類DecimalFormat format = new DecimalFormat("0.0");mCalaBtn.setVisibility(View.GONE);mResultLl.setVisibility(View.VISIBLE);mWeightBmiTv.setText(weightBmi + format.format(bmi));mWeightStateTv.setText(weightState + state);}}@Overridepublic void onCheckedChanged(RadioGroup rg, int arg1) {int id = rg.getCheckedRadioButtonId();switch (id) {case R.id.rb_who:// 選中Who單選按鈕mCurrentStandard = WHO_STANDARD;break;case R.id.rb_area:// 選中亞洲單選按鈕mCurrentStandard = AREA_STANDARD;break;default:mCurrentStandard = WHO_STANDARD;break;}}@Overrideprotected void onResume() {super.onResume();// 獲取當(dāng)前用戶選擇的標(biāo)準(zhǔn)sp = this.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);mCurrentStandard = sp.getInt("standard", WHO_STANDARD);// 根據(jù)用戶上次選擇的標(biāo)準(zhǔn)初始化單選框的選擇狀態(tài)switch (mCurrentStandard) {case WHO_STANDARD:mWhoStandardRb.setChecked(true);mAreaStandardRb.setChecked(false);break;case AREA_STANDARD:mWhoStandardRb.setChecked(false);mAreaStandardRb.setChecked(true);break;default:mWhoStandardRb.setChecked(true);mAreaStandardRb.setChecked(false);break;}}@Overrideprotected void onPause() {super.onPause();// 保存當(dāng)前用戶選擇的標(biāo)準(zhǔn)sp = this.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);Editor editor = sp.edit();editor.putInt("standard", mCurrentStandard);editor.commit();} }源碼下載
下載地址:http://download.csdn.net/detail/kedi_study/8935445
總結(jié)
以上是生活随笔為你收集整理的【BMI指数计算器V2.0】项目实战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL--PUBS
- 下一篇: Ettercap系列IV:闭嘴吧天猫精灵