2. 刪除預設的TextView
3. 新增2個EditText, 1個Button, 1個TextView
4. 修改Text屬性
5. 輸入程式
package com.example.sum;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SumActivity extends Activity {
/** Called when the activity is first created. */
EditText et1, et2;
Button bt;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(R.id.EditText01);
et2 = (EditText) findViewById(R.id.EditText02);
tv = (TextView) findViewById(R.id.TextView01);
bt = (Button) findViewById(R.id.Button01);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
double d1,d2;
d1 = Double.parseDouble(et1.getText().toString());
d2 = Double.parseDouble(et2.getText().toString());
tv.setText(Double.toString(d1+d2));
}
});
}
}
6. 執行結果
你好,不好意思我是超級初學者,程式在這段:
回覆刪除bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
說有錯誤,請問我需要改哪裡呢?
檢查一下括號
回覆刪除