2011年6月3日 星期五
6/3-Android匯率
根據上個禮拜,建立一個新專案Change,將下列的程式分別複製貼上到eclipse中
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>
Change.java
package com.android.change;
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 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("28.5");
ed2.setText("100");
btn1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
Double no1= Double.valueOf(ed1.getText().toString());
Double no2= Double.valueOf(ed1.getText().toString());
String sum= String.valueOf(no1*no2);
tv1.setText(sum);
btn1.setWidth(70);
// 按鈕事件,處理數學換算的語法
//NumberFormat nf = NumberFormat.getInstance();
//nf.setMaximumFractionDigits( 2 );
//double d = Double.parseDouble(ed2.getText().toString()) * Double.parseDouble(ed1.getText().toString());
//tv1.setText( Change.this.getResources().getString(R.string.str3) + ":" + nf.format(d) );
}
});
}
}
訂閱:
文章 (Atom)