作业4
<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.js.MainActivity" android:orientation="vertical"> <TextView android:id="@+id/textView2" android:layout_width="80dp" android:layout_height="20dp" android:layout_alignRight="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="50dp" android:text="输入第二个" /> <TextView android:id="@+id/textView1" android:layout_width="80dp" android:layout_height="20dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="50dp" android:text="输入第一个" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/textView1" android:layout_alignParentRight="true" android:ems="10" /> <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/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_below="@+id/editText2" android:layout_marginTop="100dp" android:text="-" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/radioButton1" android:layout_marginTop="100dp" android:text="+" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/radioButton1" android:layout_below="@+id/radioButton1" android:layout_marginTop="100dp" android:text="*" /> <RadioButton android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/radioButton3" android:layout_marginTop="100dp" android:text="/" /> </RadioGroup> <TextView android:id="@+id/textView3" android:layout_width="80dp" android:layout_height="20dp" android:layout_centerVertical="true" android:layout_marginLeft="120dp" android:text="输出结果" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_alignTop="@+id/textView2" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/rg" android:layout_marginTop="40dp" android:onClick="CCC" android:text="计算" /> </RelativeLayout> package com.example.js; import java.util.Set; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setContentView(R.layout.activity_main); RadioGroup rg = (RadioGroup) findViewById(R.id.rg); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { try { EditText EditText1 = (EditText) findViewById(R.id.editText1); EditText EditText2 = (EditText) findViewById(R.id.editText2); TextView textView3 = (TextView) findViewById(R.id.textView3); String s1 = EditText1.getText().toString(); String s2 = EditText2.getText().toString(); double a = Double.parseDouble(s1); double b =Double.parseDouble(s2) ; switch (checkedId) { case R.id.radioButton1: int m1 = (int) (a - b); textView3.setText(""+m1); break; case R.id.radioButton2: int m2 = (int) (a + b); textView3.setText(""+m2); break; case R.id.radioButton3: int m3 = (int) (a * b); textView3.setText(""+m3); break; case R.id.radioButton4: int m4 = (int) (a / b); textView3.setText(""+m4); break; default: break; } } catch (Exception e) { Toast.makeText(MainActivity.this, "请输入数据", 0).show(); } } }); } public void CCC(View v) { TextView textView3 = (TextView) findViewById(R.id.textView3); EditText editText = (EditText) findViewById(R.id.editText1); EditText editText2 = (EditText) findViewById(R.id.editText2); switch (v.getId()) { case R.id.button1: textView3.setText(""); editText.setText(null); editText2.setText(null); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }