<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.homework3.MainActivity" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="52dp" android:layout_toLeftOf="@+id/tv3" android:text="输入第一个数" android:textSize="20sp" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv1" android:layout_below="@+id/tv1" android:layout_marginTop="30dp" android:text="输入第二个数" android:textSize="20sp" /> <EditText android:id="@+id/et2" android:layout_width="100sp" android:layout_height="wrap_content" android:layout_alignBottom="@+id/tv2" android:layout_toRightOf="@+id/bt1" android:ems="10" android:phoneNumber="true" android:textSize="20sp" /> <EditText android:id="@+id/et1" android:layout_width="100sp" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tv1" android:layout_alignBottom="@+id/tv1" android:layout_toRightOf="@+id/bt1" android:ems="10" android:phoneNumber="true" android:textSize="20sp" /> <RadioGroup android:id="@+id/rg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:orientation="horizontal" > <RadioButton android:id="@+id/rb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- " android:textSize="25dp" /> <RadioButton android:id="@+id/rb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv2" android:layout_centerVertical="true" android:text="* " android:textSize="25dp" /> <RadioButton android:id="@+id/rb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginRight="21dp" android:layout_toLeftOf="@+id/bt1" android:text="+ " android:textSize="25dp" /> <RadioButton android:id="@+id/rb4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/ed2" android:layout_centerVertical="true" android:text="/ " android:textSize="25dp" /> </RadioGroup> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="80dp" android:onClick="remove" android:text="清除" /> <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/bt1" android:layout_centerHorizontal="true" android:text="" android:textSize="25dp" /> </RelativeLayout>
package com.example.xf; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ActionBarActivity implements RadioGroup.OnCheckedChangeListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup rg = (RadioGroup) findViewById(R.id.rg); rg.setOnCheckedChangeListener(this); } public void remove(View v) { TextView tv3 = (TextView) findViewById(R.id.tv3); EditText et1 = (EditText) findViewById(R.id.et1); EditText et2 = (EditText) findViewById(R.id.et2); switch (v.getId()) { case R.id.bt1: tv3.setText(""); et1.setText(""); et2.setText(""); break; default: break; } } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { try { EditText et1 = (EditText) findViewById(R.id.et1); EditText et2 = (EditText) findViewById(R.id.et2); TextView tv3 = (TextView) findViewById(R.id.tv3); String s1 = et1.getText().toString(); String s2 = et2.getText().toString(); double x = Double.parseDouble(s1); double y =Double.parseDouble(s2) ; switch (checkedId) { case R.id.rb1: double b1 = x + y; tv3.setText(""+b1); break; case R.id.rb2: double b2 = x - y; tv3.setText(""+b2); break; case R.id.rb3: double b3 = x * y; tv3.setText(""+b3); break; case R.id.rb4: double b4 = x / y; tv3.setText(""+b4); break; default: break; } } catch (Exception e) { Toast.makeText(MainActivity.this, "请输入数据", 0).show(); } } }