package com.example.qqqs; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup r1=(RadioGroup)findViewById(R.id.rdg); r1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub EditText e1=(EditText)findViewById(R.id.edi1); EditText e2=(EditText)findViewById(R.id.edi2); TextView textView=(TextView)findViewById(R.id.tv3); String txt1=e1.getText().toString(); String txt2=e2.getText().toString(); double num1=Integer.parseInt(txt1); double num2=Integer.parseInt(txt2); double result=0; if (checkedId == R.id.radio1) { result=num1+num2; textView.setText("结果"+result); } else if (checkedId == R.id.radio2) { result=num1-num2; textView.setText("结果"+result); } else if (checkedId == R.id.radio3) { result=num1*num2; textView.setText("结果"+result); } else if (checkedId == R.id.radio4) { result=num1/num2; textView.setText("结果"+result); } } }); } public void CCC(View view) { Button b1=(Button)findViewById(R.id.button1); EditText et1= (EditText)findViewById(R.id.edi1); EditText et2=(EditText)findViewById(R.id.edi2); TextView textView=(TextView)findViewById(R.id.tv3); String text=new String(); text=text.replace("", ""); textView.setText(text); String et = new String(); et=et.replace("", ""); et1.setText(et); et2.setText(et); } }
<RelativeLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" 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.qqqs.MainActivity" > <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" 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_marginTop="26dp" android:text="输入第二个数" android:textSize="20sp" /> <RadioGroup android:id="@+id/rdg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginTop="100dp" android:orientation="horizontal"> <RadioButton android:id="@+id/radio1" android:layout_width="40dp" android:layout_height="20dp" android:layout_alignBaseline="@id/radio1" android:layout_alignBottom="@id/radio1" android:layout_marginLeft="20dp" android:text="+" /> <RadioButton android:id="@+id/radio2" android:layout_width="77dp" android:layout_height="78dp" android:layout_marginTop="51dp" android:layout_weight="0.37" android:text="-" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@id/radio2" android:layout_alignBottom="@id/radio2" android:layout_toRightOf="@id/radio2" android:text="*" /> <RadioButton android:id="@+id/radio4" android:layout_width="40dp" android:layout_height="20dp" android:layout_alignBaseline="@id/radio1" android:layout_alignBottom="@id/radio1" android:layout_marginRight="43dp" android:text="/" /> </RadioGroup> <EditText android:id="@+id/edi1" android:layout_width="20dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_alignTop="@id/tv1" android:layout_marginLeft="35dp" android:layout_toRightOf="@id/tv1" android:background="@drawable/eid_dg" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/edi2" android:layout_width="20dp" android:layout_height="30dp" android:layout_alignBaseline="@id/tv2" android:layout_alignBottom="@id/tv2" android:layout_alignLeft="@id/edi1" android:layout_alignRight="@id/edi1" android:background="@drawable/eid_dg" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tv3" android:layout_alignBottom="@+id/tv3" android:layout_centerHorizontal="true" android:onClick="CCC" android:text="Button" /> <TextView android:id="@+id/tv3" android:layout_width="50dp" android:layout_height="60dp" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/button1" /> </RelativeLayout>