Android计算器简单版
一、布局文件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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/number1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<RadioGroup
android:id="@+id/RadioGroup_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_x="3px"
android:layout_y="54px"
>
<RadioButton
android:id="@+id/add_RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10px"
android:text="+"
/>
<RadioButton
android:id="@+id/jian_RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10px"
android:text="-"
/>
<RadioButton
android:id="@+id/cheng_RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10px"
android:text="*"
/>
<RadioButton
android:id="@+id/chu_RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10px"
android:text="/"
/>
</RadioGroup>
<EditText
android:id="@+id/number2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/resultButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="结果"
/>
<TextView
android:id="@+id/resultTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
二、android代码功能实现
package com.cn.android;
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.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private EditText number1 = null;
private EditText number2 = null;
private Button resultButton = null;
private TextView resultTextView = null;
private RadioGroup mRadioGroup = null;
private RadioButton jiaRadioButton,jianRadioButton,chengRadioButton,chuRadioButton;
private String yunsuan;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
number1 = (EditText)findViewById(R.id.number1);
number2 = (EditText)findViewById(R.id.number2);
resultButton = (Button)findViewById(R.id.resultButton);
mRadioGroup = (RadioGroup)findViewById(R.id.RadioGroup_choice);
jiaRadioButton = (RadioButton)findViewById(R.id.add_RadioButton);
jianRadioButton = (RadioButton)findViewById(R.id.jian_RadioButton);
chengRadioButton = (RadioButton)findViewById(R.id.cheng_RadioButton);
chuRadioButton = (RadioButton)findViewById(R.id.chu_RadioButton);
resultTextView = (TextView)findViewById(R.id.resultTextView);
mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
if(arg1 == jiaRadioButton.getId()){
yunsuan = "+";
}
else if(arg1 == jianRadioButton.getId()){
yunsuan = "-";
}
else if(arg1 == chengRadioButton.getId()){
yunsuan = "*";
}
else if(arg1 == chuRadioButton.getId()){
yunsuan = "/";
}
}
});
resultButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
String num1 = number1.getText().toString();
String num2 = number2.getText().toString();
final double dnum1 = Double.parseDouble(num1);
final double dnum2 = Double.parseDouble(num2);
double resultNum = 0.0;
if(yunsuan.equals("+")){
resultNum = dnum1 + dnum2;
}
else if(yunsuan.equals("-")){
resultNum = dnum1 - dnum2;
}
else if(yunsuan.equals("*")){
resultNum = dnum1 * dnum2;
}
else if(yunsuan.equals("/")){
resultNum = dnum1 / dnum2;
}
String resultString = String.valueOf(resultNum);
resultTextView.setText(resultString);
}
});
}
}