Android作业 0923
计算器小应用
package com.example.myhomework2; import androidx.appcompat.app.AppCompatActivity; import android.app.ProgressDialog; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity{ double num1 = 0 , num2 = 0 , result = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ EditText et1 = findViewById(R.id.et1); EditText et2 = findViewById(R.id.et2); num1 = Double.parseDouble(et1.getText().toString()); num2 = Double.parseDouble(et2.getText().toString()); switch (v.getId()) { case R.id.btn1: result = num1 + num2; break; case R.id.btn2: result = num1 - num2; break; case R.id.btn3: result = num1 * num2; break; case R.id.btn4: result = num1 / num2; break; } } public void click1(View v){ TextView tv1 = findViewById(R.id.tv); /* @setProgress 设置初始进度 * @setProgressStyle 设置样式(水平进度条) * @setMax 设置进度最大值 */ final int Max = 100; final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setProgress(0); progressDialog.setTitle("正在计算,请稍等..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMax(Max); progressDialog.show(); /** * 开个线程 */ new Thread(new Runnable() { @Override public void run() { int p = 0; while (p <= Max){ try { Thread.sleep(10); p++; progressDialog.setProgress(p); }catch (Exception e){ e.printStackTrace(); } } progressDialog.cancel();//达到最大就消失 } }).start(); tv1.setText("计算结果:" + result + ""); } public void click2(View v){ EditText et1 = findViewById(R.id.et1); EditText et2 = findViewById(R.id.et2); TextView tv = findViewById(R.id.tv); et1.setText(""); et2.setText(""); tv.setText(""); } }
<?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" android:background="#789878"> <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/et1" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et2" android:onClick="click" android:text="+" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et2" android:layout_toRightOf="@id/btn1" android:onClick="click" android:text="-" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et2" android:layout_toRightOf="@id/btn2" android:onClick="click" android:text="*" /> <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et2" android:layout_toRightOf="@id/btn3" android:onClick="click" android:text="/" /> <Button android:id="@+id/btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算" android:textColor="#025588" android:background="#895865" android:layout_below="@id/btn1" android:layout_marginLeft="20dp" android:onClick="click1" /> <Button android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" android:textColor="#025588" android:background="#895865" android:layout_below="@id/btn4" android:layout_toRightOf="@id/btn5" android:layout_marginLeft="135dp" android:onClick="click2" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn5" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:text="计算结果:" /> </RelativeLayout>