//页面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数字1"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"
/>
<EditText
android:id="@+id/num1"
android:inputType="number"
android:layout_width="240sp"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数字2"
android:layout_marginLeft="10sp"
android:layout_marginTop="20sp"
/>
<EditText
android:id="@+id/num2"
android:inputType="number"
android:layout_width="240sp"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginTop="15sp"
/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5sp"
android:layout_marginLeft="10sp"
>
<RadioButton
android:id="@+id/jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加"
/>
<RadioButton
android:id="@+id/jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减"
/>
<RadioButton
android:id="@+id/cheng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乘"
/>
<RadioButton
android:id="@+id/chu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="除"
/>
</RadioGroup>
<Button
android:id="@+id/btncomput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算"
android:textSize="12sp"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算结果"
android:layout_marginLeft="10sp"
android:layout_marginTop="15sp"
/>
<EditText
android:id="@+id/result"
android:inputType="number"
android:layout_width="240sp"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_marginTop="10sp"
/>
</LinearLayout>
//加页面代码
public class MainActivity extends Activity {
//定义变量
private EditText et1,et2,etRes;
private Button btn;
int results;
private RadioGroup group;
private RadioButton r1,r2,r3,r4;
private int num1;
private int num2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得ID
et1 = (EditText) findViewById(R.id.num1);
et2 = (EditText) findViewById(R.id.num2);
etRes = (EditText) findViewById(R.id.result);
btn = (Button) findViewById(R.id.btncomput);
group = (RadioGroup) findViewById(R.id.rg);
r1 = (RadioButton) findViewById(R.id.jia);
r2 = (RadioButton) findViewById(R.id.jian);
r3 = (RadioButton) findViewById(R.id.cheng);
r4 = (RadioButton) findViewById(R.id.chu);
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
num1 = Integer.parseInt(et1.getText().toString());
num2 = Integer.parseInt(et2.getText().toString());
switch(checkedId)
{
case R.id.jia:
results = num1+num2;
break;
case R.id.jian:
results = num1-num2;
break;
case R.id.cheng:
results = num1*num2;
break;
case R.id.chu:
results = num1/num2;
break;
}
}
});
//监听Button按钮
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//btn = (Button) v;
//int results = Integer.parseInt(etRes.getText().toString());
etRes.setText(results);
Toast.makeText(MainActivity.this, etRes.getText().toString(), Toast.LENGTH_LONG).show();
//Toast.makeText(MainActivity.this, etRes.setText(results), Toast.LENGTH_LONG).show();
}
} );
}