根據《深入淺出Android》中的例子,簡單熟悉Android的開發,對原書中的實例加以簡單擴展。
apk文件下載。
該程序是用於計算體重指數(BMI)的,"體重指數"是用來衡量人體體重是否正常(正常還是或胖或瘦)的一種計算方法,這種方法將人的體重和身高作為主要的計算依據。
程序主要涉及到Activity,Toast,Menu,Button,AlertDialog,TextView,EditView,RadioButton,RadioGroup等簡單控件及SharedPreferences的使用的相關知識。
程序主界面截圖如下:
主界面layout文件如下:
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:fadeScrollbars ="true"
android:orientation ="vertical"
android:scrollbars ="vertical" >
< RadioGroup
android:id ="@+id/rgSex"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:orientation ="horizontal" >
< RadioButton
android:id ="@+id/rbMale"
android:layout_width ="60dp"
android:layout_height ="wrap_content"
android:checked ="true"
android:text ="@string/male" />
< RadioButton
android:id ="@+id/rbFemale"
android:layout_width ="60dp"
android:layout_height ="wrap_content"
android:text ="@string/female" />
</ RadioGroup >
< TextView
android:id ="@+id/tvHeight"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/height" />
< EditText
android:id ="@+id/etHeight"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:inputType ="numberDecimal" />
< TextView
android:id ="@+id/tvWeigth"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/weight" />
< EditText
android:id ="@+id/EtWeight"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:inputType ="numberDecimal" />
< RelativeLayout
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_gravity ="center"
android:layout_marginTop ="@dimen/marginTop" >
< Button
android:id ="@+id/btnCompute"
android:layout_width ="wrap_content"
android:layout_height ="@dimen/buttonHeight"
android:text ="@string/btncompute" />
< Button
android:id ="@+id/btnMore"
android:layout_width ="wrap_content"
android:layout_height ="@dimen/buttonHeight"
android:layout_toRightOf ="@id/btnCompute"
android:text ="@string/btnMore" />
< Button
android:id ="@+id/btnAboutauthor"
android:layout_width ="wrap_content"
android:layout_height ="@dimen/buttonHeight"
android:layout_toRightOf ="@id/btnMore"
android:text ="@string/btnAboutauthor" />
< Button
android:id ="@+id/btnExit"
android:layout_width ="wrap_content"
android:layout_height ="@dimen/buttonHeight"
android:layout_toRightOf ="@id/btnAboutauthor"
android:text ="@string/btnExit" />
</ RelativeLayout >
< TextView
android:id ="@+id/tvNotice"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_marginTop ="@dimen/marginTop"
android:text ="@string/notice"
android:textColor ="#FF0000"
android:textStyle ="bold" />
</ LinearLayout >
在layout中定義好的控件通過findViewById方法查找到,再設置好相應的setOnClickListener處理點擊操作即可。
點擊“說明”,通過startActivity方法彈出說明界面,如下圖所示:
layout文件如下:
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent"
android:fadeScrollbars ="true"
android:orientation ="vertical"
android:scrollbars ="vertical" >
< RelativeLayout
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_marginBottom ="0dp"
android:background ="#535252" >
< TextView
android:id ="@+id/tvAboutTitle"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_alignParentLeft ="true"
android:layout_centerVertical ="true"
android:layout_marginRight ="40dp"
android:text ="@string/aboutTitle"
android:textColor ="#C2D560" />
< Button
android:id ="@+id/btnReturn"
android:layout_width ="wrap_content"
android:layout_height ="@dimen/buttonHeight"
android:layout_alignParentRight ="true"
android:text ="@string/btnReturn"
android:textColor ="#C2D560"
android:background ="#535252" />
</ RelativeLayout >
< TextView
android:id ="@+id/tvAbout"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_marginTop ="@dimen/marginTop"
android:text ="@string/about" />
< TextView
android:id ="@+id/tvFormula"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_marginTop ="@dimen/marginTop"
android:text ="@string/formula" />
< TextView
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_marginTop ="@dimen/marginTop"
android:text ="@string/standNotice"
android:textColor ="#00FFFF" />
< TextView
android:id ="@+id/tvLight"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_marginTop ="@dimen/marginTop"
android:text ="@string/light" />
< TextView
android:id ="@+id/tvExample"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_marginTop ="@dimen/marginTop"
android:text ="@string/example" />
</ LinearLayout >
點擊“關於”時,用AlertDialog彈出關於信息,當然也可以用Toast提示信息,代碼如下:
new AlertDialog.Builder(BMIActivity. this )
.setCancelable( true )
.setTitle(R.string.contactTitle)
.setMessage(R.string.contact)
.setPositiveButton(R.string.btnSure,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.cancel();
}
}).show();
// Toast.makeText(this, R.string.contact, Toast.LENGTH_SHORT).show();
}
點擊“退出”時,用AlertDialog彈出確認操作信息,代碼如下:
new AlertDialog.Builder(BMIActivity. this )
.setTitle(R.string.exitNotice)
.setCancelable( false )
.setMessage(R.string.exitAsk)
.setNegativeButton(R.string.btnReturn,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
arg0.cancel();
}
})
.setPositiveButton(R.string.btnSure,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
finish();
}
}).show();
}
通過重載onCreateOptionsMenu方法添加菜單,代碼如下:
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super .onCreateOptionsMenu(menu);
menu.add(0, menuAbout, 0, "關於……");
menu.add(0, menuExit, 0, "退出……");
return true ;
}
通過重載onOptionsItemSelected響應菜單操作,代碼如下:
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super .onOptionsItemSelected(item);
switch (item.getItemId()) {
case menuAbout:
AboutAuthor();
break ;
case menuExit:
CloseMethod();
break ;
default :
break ;
}
return true ;
}
因為自己的身高一般不常改變,要想在程序關閉后再次打開時,不需要再重復輸入身高信息,那么就需要將身高信息保存起來。Android中可以用SharedPreferences來方便保存數據。SharedPreferences是一種輕型的數據存儲方式,它的本質是基於XML文件存儲key-value鍵值對數據,通常用來存儲一些簡單的配置信息。其存儲位置在/data/data/<包名>/shared_prefs目錄下。SharedPreferences對象本身只能獲取數據而不支持存儲和修改,存儲修改是通過Editor對象實現。實現SharedPreferences存儲的步驟如下:
一、根據Context獲取SharedPreferences對象
二、利用edit()方法獲取Editor對象。
三、通過Editor對象存儲key-value鍵值對數據。
四、通過commit()方法提交數據。
本程序中通過重載onStop方法在程序退出時來保存身高數據,代碼如下:
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
SharedPreferences pre = getSharedPreferences(PREF, 0);
pre.edit().putString(PREF_HEIGHT, tvHeight.getText().toString())
.commit();
}
可以看到/data/data/<waterfrost.com.bmi>/shared_prefs目錄下生成一個名為:BMIPREF.xml的文件,如下圖所示:
BMIPREF.xml文件內容為:
<map>
<string name="BMIHEIGHT">170</string>
</map>
程序再次打開時,讀取身高數據並通過EditText的setText方法將身高設置到輸入框中,代碼如下:
SharedPreferences pre = getSharedPreferences(PREF, 0);
String height = pre.getString(PREF_HEIGHT, "");
if (!"".equals(height)) {
tvHeight.setText(height);
tvWeight.requestFocus();
}
}
另外,程序的圖標可以在AndroidManifest.xml中application節點的android:icon屬性進行設置,如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="waterfrost.com.bmi"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/bmi"
android:label="@string/app_name" >
<activity
android:name=".BMIActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AboutActivity" android:label="@string/aboutTitle" />
</application>
</manifest>
下一篇:Android手機上簡單的自動重復撥號小軟件
這個東東在春節電話訂票時能夠很好派上用場。