第四次作业
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#ffffff">
<LinearLayout
android:id="@+id/L1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="150dp"
android:layout_marginRight="30sp"
android:layout_marginLeft="30sp"
android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入第一个数"
android:textSize="30sp"
android:textColor="#000000"/>
<EditText
android:id="@+id/L11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:numeric="integer"
android:text=""/>
</LinearLayout>
<LinearLayout
android:id="@+id/L2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/L1"
android:layout_marginTop="40dp"
android:layout_marginLeft="30sp"
android:layout_marginRight="30sp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入第二个数"
android:textSize="30sp"
android:textColor="#000000" />
<EditText
android:id="@+id/L21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:background="#ffffff"
android:numeric="integer"
android:text=""/>
</LinearLayout>
<RadioGroup
android:id="@+id/sm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/L2"
android:layout_marginTop="50dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_weight="1"
android:textSize="40sp"/>
<RadioButton
android:id="@+id/jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" -"
android:layout_weight="1"
android:textSize="40sp"/>
<RadioButton
android:id="@+id/cheng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" *"
android:layout_weight="1"
android:textSize="40sp"/>
<RadioButton
android:id="@+id/chu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:layout_weight="1"
android:textSize="40sp"/>
</RadioGroup>
<TextView
android:id="@+id/jg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginRight="30sp"
android:layout_marginLeft="30sp"
android:layout_below="@+id/sm"
android:layout_marginTop="40dp"
android:text="运算结果为:"
android:textSize="30sp"/>
<Button
android:id="@+id/qk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空"
android:textSize="40sp"
android:layout_below="@+id/jg"
android:layout_centerHorizontal="true"
android:layout_marginTop="40sp"
android:onClick="dm"/>
</RelativeLayout>
MainActivity
package com.example.jianyijisuanqi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public RadioGroup radioGroup;
EditText L111;
EditText L211;
TextView jg;
Integer x;
Integer y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=findViewById(R.id.sm);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch(i){
case R.id.jia:jia();break;
case R.id.jian:jian();break;
case R.id.cheng:cheng();break;
case R.id.chu:chu();break;
}
}
});
}
private void jia(){
jg=findViewById(R.id.jg);
L111=findViewById(R.id.L11);
L211=findViewById(R.id.L21);
String L1=L111.getText().toString();
String L2=L211.getText().toString();
x=Integer.valueOf(L1);
y=Integer.valueOf(L2);
String st=Integer.toString(x+y);
jg.setText(st);
}
private void jian(){
jg=findViewById(R.id.jg);
L111=findViewById(R.id.L11);
L211=findViewById(R.id.L21);
String L1=L111.getText().toString();
String L2=L211.getText().toString();
x=Integer.valueOf(L1);
y=Integer.valueOf(L2);
String st=Integer.toString(x-y);
jg.setText(st);
}
private void cheng(){
jg=findViewById(R.id.jg);
L111=findViewById(R.id.L11);
L211=findViewById(R.id.L21);
String L1=L111.getText().toString();
String L2=L211.getText().toString();
x=Integer.valueOf(L1);
y=Integer.valueOf(L2);
String st=Integer.toString(x*y);
jg.setText(st);
}
private void chu(){
jg=findViewById(R.id.jg);
L111=findViewById(R.id.L11);
L211=findViewById(R.id.L21);
String L1=L111.getText().toString();
String L2=L211.getText().toString();
x=Integer.valueOf(L1);
y=Integer.valueOf(L2);
if(x==0||y==0){
Toast.makeText(MainActivity.this,"除数不能为0",Toast.LENGTH_SHORT).show();
}else{
String st=Integer.toString(x/y);
jg.setText(st);
}
}
public void dm(View view){
jg.setText("");
L111.setText("");
L211.setText("");
}
}





浙公网安备 33010602011771号