studentPDY

第四次作业

<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=".MainActivity" >
<EditText
android:id="@+id/EditText01"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText2"
android:layout_marginTop="20dp"
android:ems="10" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/EditText01"
android:layout_toLeftOf="@+id/EditText01"
android:text="输入第二个数" />
<EditText
android:id="@+id/editText2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/EditText01"
android:layout_alignParentTop="true"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText2"
android:layout_toLeftOf="@+id/editText2"
android:text="输入第一个数" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView2"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/RE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="53dp"
android:onClick="click"
android:text="清空" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/RE"
android:layout_alignLeft="@+id/TextView01"
android:layout_marginBottom="78dp"
android:text="计算结果是" />
<RadioGroup
android:id="@+id/rdg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignLeft="@+id/textView2"
android:orientation="vertical" >
<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/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="/" />
<RadioButton
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/add"
android:text="-" />
</RadioGroup>
</RelativeLayout>

package com.example.jisuan;
import com.example.jisuan.R;
import com.example.jisuan.MainActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
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 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 void onCheckedChanged(RadioGroup group, int checkedId) {
EditText et1 = (EditText) findViewById(R.id.editText2);
EditText et2 = (EditText) findViewById(R.id.EditText01);
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.tv);
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.tv);
TextView tv2 = (TextView) findViewById(R.id.editText2);
;
TextView tv3 = (TextView) findViewById(R.id.EditText01);
;
switch (view.getId()) {
case R.id.RE:
tv.setText("");
tv2.setText("");
tv3.setText("");
}

      

 

  

 

posted on 2019-09-17 20:58  studentPDY  阅读(...)  评论(...编辑  收藏

My Links

Blog Stats