package com.example.test3; import android.app.AliasActivity; import android.app.ProgressDialog; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AliasActivity{ 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 = (EditText) findViewById(R.id.et1); EditText et2 = (EditText) 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 = (TextView) 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 = (EditText) findViewById(R.id.et1); EditText et2 = (EditText) findViewById(R.id.et2); TextView tv = (TextView) findViewById(R.id.tv); et1.setText(""); et2.setText(""); tv.setText(""); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#786678" > <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:layout_below="@id/btn1" android:layout_marginLeft="20dp" android:background="#895865" android:onClick="click1" android:text="计算" android:textColor="#024488" /> <Button android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn4" android:layout_marginLeft="135dp" android:layout_toRightOf="@id/btn5" android:background="#895865" android:onClick="click2" android:text="清空" android:textColor="#024488" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn5" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:text="计算结果:" /> </RelativeLayout>