04

<RelativeLayout 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: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.jsj.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:textSize="20sp"
        android:text="输入第一个数:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignTop="@+id/textView1"
        android:background="@null"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="10dp"
        android:text="输入第二个数:"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignTop="@+id/textView2"
        android:background="@null"
        android:ems="10" />
   <RadioGroup 
       android:id="@+id/rdg"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:layout_below="@+id/textView2"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="15dp"
       >
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="*" />

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/" />
   RadioGroup>

   <TextView
       android:id="@+id/textView3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/rdg"
       android:layout_centerHorizontal="true"
       android:textSize="20sp"
       android:layout_marginTop="50dp"
       android:text="计算结果为:" />

   <Button
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/textView3"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="58dp"
       android:onClick="ccc"
       android:text="全部清除" />
    
RelativeLayout>
package com.example.jsj;

import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity implements OnClickListener {
    private RadioGroup radioGroup;
    private TextView textView;
    EditText num1, num2;
    private Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.rdg);
        textView = (TextView) findViewById(R.id.textView3);
        num1 = (EditText) findViewById(R.id.editText1);
        num2 = (EditText) findViewById(R.id.editText2);
        bt=(Button) findViewById(R.id.button1);
        bt.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @SuppressLint("NewApi")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                switch (checkedId) {
                case R.id.radioButton1:
                    if (!num1.getText().toString().isEmpty() && !num2.getText().toString().isEmpty()) {
                        double n1 = Double.parseDouble(num1.getText().toString());
                        double n2 = Double.parseDouble(num2.getText().toString());
                        double res = n1 + n2;
                        textView.setText(String.valueOf(res));
                        break;
                    }
                case R.id.radioButton2:    
                    if (!num1.getText().toString().isEmpty() && !num2.getText().toString().isEmpty()) {
                        double n1 = Double.parseDouble(num1.getText().toString());
                        double n2 = Double.parseDouble(num2.getText().toString());
                        double res = n1 - n2;
                        textView.setText(String.valueOf(res));
                        break;
                    }
                case R.id.radioButton3:    
                    if (!num1.getText().toString().isEmpty() && !num2.getText().toString().isEmpty()) {
                        double n1 = Double.parseDouble(num1.getText().toString());
                        double n2 = Double.parseDouble(num2.getText().toString());
                        double res = n1 * n2;
                        textView.setText(String.valueOf(res));
                        break;
                    }
                case R.id.radioButton4:    
                    if (!num1.getText().toString().isEmpty() && !num2.getText().toString().isEmpty()) {
                        double n1 = Double.parseDouble(num1.getText().toString());
                        double n2 = Double.parseDouble(num2.getText().toString());
                        double res = n1 / n2;
                        textView.setText(String.valueOf(res));
                        break;
                    }    
                }
            }
        });
        
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        num1.setText(null);
        num2.setText(null);
        textView.setText(" ");
    }
}

04_第1张图片04_第2张图片04_第3张图片04_第4张图片

你可能感兴趣的:(04)