<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <EditText android:id="@+id/EditText2" android:layout_width="113dp" android:layout_height="wrap_content" android:layout_below="@+id/EditText1" android:layout_alignParentRight="true" android:layout_marginTop="17dp" android:layout_marginRight="109dp" android:ems="10" /> <TextView android:id="@+id/TextView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/EditText2" android:layout_marginRight="18dp" android:layout_marginBottom="7dp" android:layout_toLeftOf="@+id/EditText2" android:text="第二个数" /> <EditText android:id="@+id/EditText1" android:layout_width="112dp" android:layout_height="wrap_content" android:layout_alignLeft="@+id/EditText2" android:layout_marginLeft="-3dp" android:layout_marginTop="200dp" android:ems="10" /> <TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/EditText1" android:layout_marginRight="17dp" android:layout_marginBottom="6dp" android:layout_toLeftOf="@+id/EditText1" android:text="第一个数" /> <TextView android:id="@+id/jieguo" android:layout_width="138dp" android:layout_height="wrap_content" android:layout_alignTop="@+id/TextView3" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginTop="2dp" android:layout_marginEnd="90dp" android:layout_marginRight="90dp" /> <Button android:id="@+id/EMPTY" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginStart="1dp" android:layout_marginBottom="58dp" android:layout_toEndOf="@+id/rdg" android:onClick="click" android:text="EMPTY" /> <TextView android:id="@+id/TextView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/EMPTY" android:layout_alignLeft="@+id/TextView2" android:layout_marginLeft="2dp" android:layout_marginBottom="100dp" android:text="结果" /> <RadioGroup android:id="@+id/rdg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/TextView3" android:layout_alignLeft="@+id/TextView3" android:orientation="vertical" tools:ignore="UnknownId"> <RadioButton android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/rdg" android:text="+" /> <RadioButton android:id="@+id/minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/add" android:text="-" /> <RadioButton android:id="@+id/mul" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/minus" android:text="*" /> <RadioButton android:id="@+id/devide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/mul" android:text="/" /> </RadioGroup> </RelativeLayout>
package com.example.zuoyesi; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends Activity implements OnCheckedChangeListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup rg = (RadioGroup) findViewById(R.id.rdg); rg.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { EditText et1 = (EditText) findViewById(R.id.EditText1); EditText et2 = (EditText) findViewById(R.id.EditText2); String s1 = et1.getText().toString(); String s2 = et2.getText().toString(); double num1 = Integer.valueOf(s1).intValue(); double num2 = Integer.valueOf(s2).intValue(); // TODO 自动生成的方法存根 TextView tv = (TextView) findViewById(R.id.jieguo); switch (checkedId) { case R.id.add: num1 = num1 + num2; s1 = String.valueOf(num1); tv.setText(s1); break; case R.id.minus: num1 = num1 - num2; s1 = String.valueOf(num1); tv.setText(s1); break; case R.id.mul: num1 = num1 * num2; s1 = String.valueOf(num1); tv.setText(s1); break; case R.id.devide: num1 = num1 / num2; s1 = String.valueOf(num1); tv.setText(s1); break; } } public void click(View view) { TextView tv = (TextView) findViewById(R.id.jieguo); TextView tv2 = (TextView) findViewById(R.id.EditText1); ; TextView tv3 = (TextView) findViewById(R.id.EditText2); ; switch (view.getId()) { case R.id.EMPTY: tv.setText(""); tv2.setText(""); tv3.setText(""); } } }