/*
* ed1 = 匯率
* ed2 = 台幣
* 計算公式: 美金 = 台幣/匯率 => ed2/ed1
*
* 學習重點:
* 0) context.findViewById 即 EX203.this (ClassName.this)
* 1) setOn XXX Listener(),建立按鈕事件處理
* 2) new Widget.OnClickListener(){} (口訣:小括號 大括號,最末小括號後分號)
* 3) 滑鼠游標移置紅色文字線上 Add unimplemented methods
* 4) 數學換算,與資料型別轉換 in Java
* 5) 覆寫 Activity 的 onPause() 或 onStop(),將 匯率 儲存下來 (SharePreference物件)
* 6) 覆寫 Activity 的 onResume(),將 匯率 讀取出來 (SharePreference物件)
*/
package irdc.ex203;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class EX203 extends Activity
{
private EditText ed1, ed2;
private Button btn1;
private TextView tv1;
public static final String MY_PREFS = "mSharedPreferences01";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText)EX203.this.findViewById(R.id.editText1); // 匯率
ed2 = (EditText)EX203.this.findViewById(R.id.editText2); // 台幣
btn1 = (Button)EX203.this.findViewById(R.id.button1);
tv1 = (TextView)EX203.this.findViewById(R.id.textView3);
ed1.setText("33.5");
ed2.setText("10000");
btn1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 按鈕事件,處理數學換算的語法
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits( 2 );
double d = Double.parseDouble(ed2.getText().toString()) / Double.parseDouble(ed1.getText().toString());
tv1.setText( EX203.this.getResources().getString(R.string.str3) + ":" + nf.format(d) );
}
});
}/*End: onCreate()*/
@Override
protected void onResume()
{
// TODO Auto-generated method stub
SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);
// 第二個參數為 假若 "rate" 不存在,程式為第一次執行的話,要以什麼值取代
String previousInputRate = mySharedPreferences.getString("rate", "");
ed1.setText(previousInputRate);
super.onResume();
}
@Override
protected void onStop()
{
// TODO Auto-generated method stub
SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);
SharedPreferences.Editor ed = mySharedPreferences.edit();
ed.putString("rate", ed1.getText().toString() );
ed.commit();
super.onStop();
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, EX203!</string>
<string name="app_name">EX203</string>
<string name="str1">匯率換算</string>
<string name="str2">匯率</string>
<string name="str3">臺幣</string>
<string name="str_btn1">可兌換美金</string>
</resources>
===================================================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str1">
</TextView>
<EditText
android:id="@+id/editText1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="EditText" >
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str2">
</TextView>
<EditText
android:id="@+id/editText2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="EditText" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_btn1">
</Button>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str3" >
</TextView>
</LinearLayout>
===================================================================
===================================================================
package edu.fcu.sean;import java.text.NumberFormat;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Change extends Activity {
private EditText ed1, ed2;
private Button btn1;
private TextView tv1;
protected ColorStateList color;
public static final String MY_PREFS = "mSharedPreferences01";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText)Change.this.findViewById(R.id.editText1); // 匯率
ed2 = (EditText)Change.this.findViewById(R.id.editText2); // 台幣
btn1 = (Button)Change.this.findViewById(R.id.button1);
tv1 = (TextView)Change.this.findViewById(R.id.textView3);
ed1.setText("33.5");
ed2.setText("10000");
btn1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ed1.setTextColor(Color.RED);
}
});
}/*End: onCreate()*/
}
===================================================================