此範例為簡單的公尺與英尺互相換算,有興趣的朋友也可加強至換算為各項單位。
------------------------------------------GOOD2.JAVA-----------------------------------------
public class GDD02 extends Activity {
private Button Button1,Button2;
private EditText EText;
private TextView infoView;
double xx,yy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
setlistener();
}
public void findview()
{
EText = (EditText) findViewById(R.id.Height);
infoView = (TextView)findViewById(R.id.BMI_Suggest);
Button1= (Button)findViewById(R.id.btn1);
Button2= (Button)findViewById(R.id.btn2);
}
public void setlistener()
{
Button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Convert1();
}
});
Button2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Convert2();
}
});
}
private void Convert1()
{
try{
DecimalFormat cf = new DecimalFormat("0.0");
DecimalFormat nf = new DecimalFormat("0.00");
xx=Double.parseDouble(EText.getText().toString());
yy=Double.parseDouble(EText.getText().toString())*3.28;
infoView.setText(String.valueOf(cf.format(xx)+"公尺="+nf.format(yy)+"英尺"));
}
catch (NumberFormatException e)
{
infoView.setText("請輸入數字!");
}
}
private void Convert2()
{
try{
DecimalFormat cf = new DecimalFormat("0.0");
DecimalFormat nf = new DecimalFormat("0.00");
xx=Double.parseDouble(EText.getText().toString());
yy=Double.parseDouble(EText.getText().toString())*0.3048;
infoView.setText(String.valueOf(cf.format(xx)+"英尺="+nf.format(yy)+"公尺"));
}
catch (NumberFormatException e)
{
infoView.setText("請輸入數字!");
}
}
protected static final int MENU_RESET =Menu.FIRST;
protected static final int SAVE =Menu.FIRST+1;
protected static final int PUT =Menu.FIRST+2;
@Override
public boolean
onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
menu.add(0, MENU_RESET, 0, "重設");
menu.add(0, SAVE, 1, "儲存當前數值");
menu.add(0, PUT, 2, "載入偏好設定");
return true;
}
public boolean
onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId()){
case MENU_RESET:
ResetValue();
break;
case SAVE:
SAVE();
break;
case PUT:
PUT();
break;
}
return true;
}
private void ResetValue()
{
EText.setText("");
infoView.setText("");
}
private void SAVE()
{
SharedPreferences settings = getSharedPreferences("Preference", 0);
//置入屬性的字串
settings.edit().putString("name", EText.getText().toString()).commit();
settings.edit().putString("math", infoView.getText().toString()).commit();
}
private void PUT()
{
SharedPreferences settings = getSharedPreferences("Preference", 0);
//取出屬性的字串
String name = settings.getString("name", "");
EText.setText(name);
String math = settings.getString("math", "");
infoView.setText(math);
}
}
------------------------------------------main.xml-----------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget41"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/in_Height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/in_Height"
>
</TextView>
<EditText
android:id="@+id/Height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text="@string/Height"
>
</EditText>
<TextView
android:id="@+id/BMI_Result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/BMI_Result"
>
</TextView>
<TextView
android:id="@+id/BMI_Suggest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/BMI_Suggest"
>
</TextView>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/btn2" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/btn1" />
</RelativeLayout>
</LinearLayout>
------------------------------------------實際運行畫面-----------------------------------------

開始執行的畫面
輸入欲轉換的值並按下轉換的按鈕即可得到所需要的答案

按下下方MENU鍵,並按下儲存當前數值
可手動或按下MENU的重設按鈕將畫面清除。
按下MENU鍵的載入偏好設定,即可把原先儲存起來的數值及換算結果重新顯示出來至畫面。














