Bm.java
package Bm.i;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;
public class Bm extends Activity {
/** Called when the activity is first created. */
private EditText et1, et2;
private Button bt1;
private TextView tv1, tv2, tv3;
public static final String MY_PREFS = "mSharedPreferences01";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText)Bm.this.findViewById(R.id.et1);
et2 = (EditText)Bm.this.findViewById(R.id.et2);
bt1 = (Button)Bm.this.findViewById(R.id.bt1);
tv3 = (TextView)Bm.this.findViewById(R.id.tv3);
bt1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits( 2 );
EditText et1 = (EditText)findViewById(R.id.et1);
EditText et2 = (EditText)findViewById(R.id.et2);
double h = Double.parseDouble(et1.getText().toString());
double w = Double.parseDouble(et2.getText().toString());
double BMI = w / ((h/100) * (h/100));
tv3.setText("BMI:"+nf.format(BMI));
}
});
}
}
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="Height"
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<EditText
android:id="@+id/et1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="numberDecimal"
>
<requestFocus></requestFocus>
</EditText>
<TextView
android:text="Weight"
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<EditText
android:id="@+id/et2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="numberDecimal"
></EditText>
<Button
android:text="Count"
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></Button>
<TextView
android:text=" "
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
</LinearLayout>
========================================================================
Try3.java
package Try.T;
import java.text.NumberFormat;
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 Try3 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)Try3.this.findViewById(R.id.editText1); // 匯率
ed2 = (EditText)Try3.this.findViewById(R.id.editText2); // 台幣
btn1 = (Button)Try3.this.findViewById(R.id.button1);
tv1 = (TextView)Try3.this.findViewById(R.id.textView3);
ed1.setText("33.5");
ed2.setText("10000");
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) );
}
});
}
}
strings.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>
</resources>
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>