package edu.zone.sun.calc;
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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 Calc extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
//Listen for button clicks
setListener();
}
private EditText fieldfirst;
private EditText fieldsecond;
private TextView result;
private Button button;
private DecimalFormat df;
private double first;
private double second;
private double sum;
private void setListener() {
button.setOnClickListener(CalcB);
}
private void findView() {
fieldfirst=(EditText)findViewById(R.id.first);
fieldsecond=(EditText)findViewById(R.id.second);
button=(Button)findViewById(R.id.button);
}
private OnClickListener CalcB=new OnClickListener() {
@Override
public void onClick(View v) {
df=new DecimalFormat("0.00");
first=Double.parseDouble(fieldfirst.getText().toString());
second=Double.parseDouble(fieldsecond.getText().toString());
sum=first+second;
result=(TextView)findViewById(R.id.result);
//result.setText("你的计算结果是:"+df.format(sum));
result.setText(getText(R.string.calcresult)+df.format(sum)); //R.string.calcresult不能和数字连接
openOptionsDialog();
}
};
//计算结果后,弹出对话框(通过AlertDialog实现的)
private void openOptionsDialog(){
new AlertDialog.Builder(Calc.this)
.setTitle(R.string.about)
.setMessage(R.string.message)
.setPositiveButton(R.string.label_ok,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
}
- 1
- 2
- 3
- 4
前往页