首先,建立一個專案(Project)名為 Change
在Change→com.android.change→Change.java的地方
貼上
package com.android.change;
import android.app.Activity;
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;
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");
Double nu1= Double.valueOf(ed1.getText().toString());
Double nu2= Double.valueOf(ed2.getText().toString());
String sum=String.valueOf(nu2/nu1);
tv1.setText(sum);
btn1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
btn1.setWidth(70);
// 按鈕事件,處理數學換算的語法
//NumberFormat nf = NumberFormat.getInstance();
//nf.setMaximumFractionDigits( 2 );
//double d = Double.parseDouble(ed2.getText().toString()) / Double.parseDouble(ed1.getText().toString());
//tv1.setText( Try3.this.getResources().getString(R.string.str3) + ":" + nf.format(d) );
}
});
}
}
在Change→layout→main.xml的地方
貼上
<?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:text="@string/str2"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:text=""
android:layout_width="match_parent">
</EditText>
<TextView
android:text="@string/str3"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_width="wrap_content">
</TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:text=""
android:layout_width="match_parent">
</EditText>
<Button android:text="計算台幣兌換美金" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView
android:text="@string/str_btn1"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
在Change→values→main.xml的地方
貼上
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, EX203!</string>
<string name="app_name">匯率換算</string>
<string name="str1">匯率換算</string>
<string name="str2">匯率</string>
<string name="str3">臺幣</string>
<string name="str_btn1">可兌換美金</string>
<string name="String00">匯率</string>
</resources>
完成!!