看板 AndroidDev 關於我們 聯絡資訊
※ 引述《femlro (主戰場:股版)》之銘言: : 總算把BMI給搞定了 : 可以在安卓測試了 : 寫一下遇到問題的過程 : 目前是參考網路上的範例發現 : 會遇到的問題應該是版本的關係 : 目前我在2013/10/26測試這個調成過後ok了 : 但是很多涵義還是不懂 : 第一步是先設定字串 : 在res\value\string先輸入 : <?xml version="1.0" encoding="utf-8"?> : <resources> : <string name="action_settings">title</string> : <string name="app_name">BMI</string> : <string name="menu_settings">Settings</string> : <string name="title_activity_bmi">BMI</string> : <string name="bmi_height">身高 (cm)</string> : <string name="bmi_weight">體重 (kg)</string> : <string name="bmi_calculate">計算 BMI 值</string> : </resources> : 我發現 : action_settings這行好像是大問題 : 範例有的沒加 : 如果不加會導致R.java讀不到 : 問題很大 : 想請高手解釋這一行為什麼會影響R.java R檔讀不到是指R檔無法生成嗎? 如果是無法生成 你把<string name="menu_settings">Settings</string> 這行拿掉 就可以看到有檔案變成紅色的 我建立一個新專案 拿掉這行 看到的是menu.xml這個檔案變紅色 因為你拿掉menu_settings這個變數 所以menu.xml會找不到 而無法生成R檔 : 接著設定BMI出來以後的advice(建議) : 要在 : res\value : 底下新增 : xml 甚麼樣的xml? string.xml? string.xml是本來就有了 : 這一個動作好像新版的有更改 : <?xml version="1.0" encoding="UTF-8"?> : <resources> : <string name="advice_light">你該多吃點</string> : <string name="advice_average">體型很棒喔</string> : <string name="advice_heavy">你該節食了</string> : </resources> 其實看不太懂這段敘述 我猜應該是要講 為什麼要將字串獨立出來這個問題吧? 假設今天有一百個元件, 每一個字串都是寫在元件的xml宣告 假設今天老闆突然說要變動這一百個元件的名稱 你可能要改好幾個小時 也許還會有遺漏 如果你獨立出來宣告成一個文字變數 所有元件只要去讀取@string/xxx 未來如果要有所變動 則只需改一行即可 : 接著回到layout : 把以下這些程式碼放進去 : <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" : xmlns:tools="http://schemas.android.com/tools" : android:layout_width="match_parent" : android:layout_height="match_parent" > : <TextView : android:id="@+id/textHeight" : android:layout_width="wrap_content" : android:layout_height="wrap_content" : android:text="@string/bmi_height" /> : <EditText : android:id="@+id/inputHeight" : style="editTextStyle" : android:layout_width="fill_parent" : android:layout_height="wrap_content" : android:layout_alignParentLeft="true" : android:layout_below="@+id/textHeight" : android:inputType="number" /> : <TextView : android:id="@+id/textWeight" : android:layout_width="fill_parent" : android:layout_height="wrap_content" : android:layout_alignParentLeft="true" : android:layout_below="@+id/inputHeight" : android:text="@string/bmi_weight" /> : <EditText : android:id="@+id/inputWeight" : android:layout_width="fill_parent" : android:layout_height="wrap_content" : android:layout_alignParentLeft="true" : android:layout_below="@+id/textWeight" : android:ems="10" : android:inputType="number" /> : <Button : android:id="@+id/buttonCalculate" : android:layout_width="fill_parent" : android:layout_height="wrap_content" : android:layout_alignParentLeft="true" : android:layout_below="@+id/inputWeight" : android:text="@string/bmi_calculate" /> : <TextView : android:id="@+id/textResult" : android:layout_width="fill_parent" : android:layout_height="wrap_content" : android:layout_alignParentLeft="true" : android:layout_below="@+id/buttonCalculate" /> : <TextView : android:id="@+id/textSuggest" : android:layout_width="fill_parent" : android:layout_height="wrap_content" : android:layout_alignParentLeft="true" : android:layout_below="@+id/textResult" /> : </RelativeLayout> : 最後最容易有問題的就是這段 上面只是排版的宣告 : 想請高手解釋這段的語言 : package com.practice.bmi; : import java.text.DecimalFormat; : import android.os.Bundle; : import android.app.Activity; : import android.view.Menu; : import android.view.View; : import android.view.View.OnClickListener; : import android.widget.Button; : import android.widget.EditText; : import android.widget.TextView; 這邊只是把需要的套件帶進這隻程式 : public class BMI extends Activity { : @Override : public void onCreate(Bundle savedInstanceState) { 程式都會有一個程式進入點 在android的activity裡面 進入點就是onCreate : super.onCreate(savedInstanceState); : setContentView(R.layout.activity_bmi); 將你宣告的xml版面帶進這隻程式 方便用程式來控制 : //Listen for button clicks : Button button = (Button)findViewById(R.id.buttonCalculate); 將你宣告的button從xml撈出 丟給button這個變數 : button.setOnClickListener(calcBMI); 將你的button設定事件 也就是說當你按下button 會觸發的事情是什麼 : } : @Override : public boolean onCreateOptionsMenu(Menu menu) { : getMenuInflater().inflate(R.menu.bmi, menu); : return true; : } 這邊是控制你的menu 程式預設的bmi那本後面會教 : private OnClickListener calcBMI = new OnClickListener() : { : public void onClick(View v) : { 這邊就是我前面講的button按下去 會發生甚麼事情 : DecimalFormat nf = new DecimalFormat("0.00"); 設定你的數字格式到小數點第二位 後面會用到 : EditText fieldheight = (EditText)findViewById(R.id.inputHeight); 取出使用者輸入的高度 : EditText fieldweight = (EditText)findViewById(R.id.inputWeight); 取出使用者輸入的體重 : double height = : Double.parseDouble(fieldheight.getText().toString())/100; 取出的高度是文字 必須轉乘數字進行計算 並且存入變數 方便後面bmi的計算 : double weight = : Double.parseDouble(fieldweight.getText().toString()); 取出的重量是文字 必須轉乘數字進行計算 並且存入變數 方便後面bmi的計算 : double BMI = weight / (height * height); 計算bmi : TextView result = (TextView)findViewById(R.id.textResult); 這個元件是用來顯示結果 : result.setText("Your BMI is " + nf.format(BMI)); 將上面的元件拿來顯示bmi的結果 : //Give health advice : TextView fieldsuggest = : (TextView)findViewById(R.id.textSuggest); : if(BMI>25){ : fieldsuggest.setText(R.string.advice_heavy); 假設bmi超過25就顯示advice_heavy這個字串內的內容 在string.xml那邊可以看到 : }else if(BMI<20){ : fieldsuggest.setText(R.string.advice_light); 假設bmi超過20就顯示advice_light這個字串內的內容 在string.xml那邊可以看到 : }else { : fieldsuggest.setText(R.string.advice_average); 假設上面的情況都沒發生 就跳到這個判斷式 顯示advice_average這個字串內的內容 : } : } 以上 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 36.236.172.36
qweqweqweqwe:字串寫在xml主要是因為多國語言比較方便吧! 10/27 00:19
qweqweqweqwe:如果只是為了幾百個元件用一個字串的話 那大可直接 10/27 00:19
qweqweqweqwe:宣告一個static final String 然後大家用他就夠了 10/27 00:20
不 元件初始化的時候 在XML不可能用這個方法 而且假設要設定每個元件的文字 你必須要findviewbyid出每個元件 在進行設定 豈不更麻煩
qweqweqweqwe:另外依據自己的經驗xml的檔名並不重要 方便管理而已 10/27 00:24
看不懂這句話的意思?
qweqweqweqwe:不過沒想到f大在研究Android XD 10/27 00:27
對啊 XD ※ 編輯: givemepass 來自: 221.120.6.134 (10/27 10:21)
asilzheng:前兩句說的是你可以在string.xml內加<color .../>這類 10/27 15:47