作业四
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/line1" android:layout_marginTop="80dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="18sp" android:layout_weight="1" android:text="请输入第一个数" /> <EditText android:id="@+id/lin1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/line2" android:layout_below="@id/line1" android:layout_marginTop="10dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="18sp" android:layout_weight="1" android:text="请输入第二个数" /> <EditText android:id="@+id/lin2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/line3" android:layout_below="@id/line2" android:layout_marginTop="10dp"> <RadioGroup android:id="@+id/radio" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/jia" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20sp" android:layout_weight="1" android:text="+"/> <RadioButton android:id="@+id/jian" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20sp" android:layout_weight="1" android:text="-"/> <RadioButton android:id="@+id/cheng" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20sp" android:layout_weight="1" android:text="*"/> <RadioButton android:id="@+id/chu" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20sp" android:layout_weight="1" android:text="/"/> </RadioGroup> </LinearLayout> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:layout_below="@id/line3" android:layout_centerInParent="true" android:layout_marginTop="10dp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_centerInParent="true" android:layout_below="@id/text" android:layout_marginTop="30dp" /> </RelativeLayout>
package cn.itcase.calc; import androidx.appcompat.app.AppCompatActivity; 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 AppCompatActivity { private RadioGroup radioGroup; private TextView textView; private EditText editText1,editText2; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup = (RadioGroup) findViewById(R.id.radio); textView = (TextView) findViewById(R.id.text); editText1 = (EditText) findViewById(R.id.lin1); editText2 = (EditText) findViewById(R.id.lin2); button=(Button)findViewById(R.id.button1) ; button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { editText1.setText(""); editText2.setText(""); textView.setText(""); } }); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged (RadioGroup group,int checkId){ if (checkId == R.id.jia) { String jiaf1 = editText1.getText().toString(); Double jiaf4= Double.parseDouble(jiaf1); String jiaf2 = editText2.getText().toString(); Double jiad3=Double.parseDouble(jiaf2); Double jiaf5=jiad3+jiaf4; textView.setText("计算结果为" +jiaf5); } else if (checkId == R.id.jian) { String jianf1 = editText1.getText().toString(); Double jianf4= Double.parseDouble(jianf1); String jianf2 = editText2.getText().toString(); Double jiand3=Double.parseDouble(jianf2); Double jianf5=jianf4-jiand3; textView.setText("计算结果为" +jianf5); } else if (checkId == R.id.cheng) { String chengf1 = editText1.getText().toString(); Double chengf4= Double.parseDouble(chengf1); String chengf2 = editText2.getText().toString(); Double chengd3=Double.parseDouble(chengf2); Double chengf5=chengd3*chengf4; textView.setText("计算结果为" +chengf5); } else if (checkId == R.id.chu) { String chuf1 = editText1.getText().toString(); Double chuf4= Double.parseDouble(chuf1); String chuf2 = editText2.getText().toString(); Double chud3=Double.parseDouble(chuf2); Double chuf5=chuf4/chud3; textView.setText("计算结果为" +chuf5); } } }); } }