Android實(shí)現(xiàn)簡易計(jì)算功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡易計(jì)算功能的具體代碼,供大家參考,具體內(nèi)容如下
效果如圖:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="clip_horizontal"
android:orientation="vertical"
android:padding="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="操作數(shù):"
android:textSize="20sp" />
<EditText
android:id="@+id/firstNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請(qǐng)輸入數(shù)值操作數(shù)"
android:textStyle="bold"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="算術(shù)運(yùn)算:"
android:textSize="20sp">
</TextView>
<Spinner
android:id="@+id/operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/sign" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="操作數(shù):"
android:textSize="20sp" />
<EditText
android:id="@+id/secondNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="請(qǐng)輸入數(shù)值操作數(shù)"
android:textStyle="bold"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="等于:" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="計(jì)算結(jié)果"
android:padding="15dp"
android:textColor="#F44336"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sign">
<item>請(qǐng)選擇運(yùn)算符</item>
<item>+</item>
<item>-</item>
<item>*</item>
<item>/</item>
</string-array>
</resources>
MainActivity
package com.jld.homework;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String op;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) this.findViewById(R.id.operator);//獲取活動(dòng)布局中的Spinner對(duì)象
//為Spinner注冊(cè)內(nèi)部監(jiān)聽器對(duì)象
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//將Spinner選項(xiàng)的值賦值給成員變量op(保存算術(shù)運(yùn)算符)
op = ((TextView) view).getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//各類組件
Button calcButton = findViewById(R.id.calc);
EditText firstNum = findViewById(R.id.firstNum);
EditText secondNum = findViewById(R.id.secondNum);
TextView result = findViewById(R.id.result);
//計(jì)算核心
calcButton.setOnClickListener(v -> {
switch (op) {
case "+": {
double r = Double.parseDouble(firstNum.getText().toString()) + Double.parseDouble(secondNum.getText().toString());
result.setText(String.valueOf(r));
break;
}
case "-": {
double r = Double.parseDouble(firstNum.getText().toString()) - Double.parseDouble(secondNum.getText().toString());
result.setText(String.valueOf(r));
break;
}
case "*": {
double r = Double.parseDouble(firstNum.getText().toString()) * Double.parseDouble(secondNum.getText().toString());
result.setText(String.valueOf(r));
break;
}
case "/": {
double r = Double.parseDouble(firstNum.getText().toString()) / Double.parseDouble(secondNum.getText().toString());
result.setText(String.valueOf(r));
break;
}
default://非法情況報(bào)錯(cuò)
result.setText(R.string.ERROR);
break;
}
});
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 從零開始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
- android計(jì)算pad或手機(jī)的分辨率/像素/密度/屏幕尺寸/DPI值的方法
- Android開發(fā)實(shí)現(xiàn)的簡單計(jì)算器功能【附完整demo源碼下載】
- android計(jì)算器簡單實(shí)現(xiàn)代碼
- Android計(jì)算器編寫代碼
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- Android Studio實(shí)現(xiàn)簡易計(jì)算器
- Android獲取經(jīng)緯度計(jì)算距離介紹
- Android進(jìn)階之使用時(shí)間戳計(jì)算時(shí)間差
- Android中使用GridLayout網(wǎng)格布局來制作簡單的計(jì)算器App
相關(guān)文章
android ImageView 的幾點(diǎn)經(jīng)驗(yàn)總結(jié)
本篇文章是對(duì)android中ImageView的使用技巧進(jìn)行了幾點(diǎn)經(jīng)驗(yàn)總結(jié),需要的朋友參考下2013-06-06
Android應(yīng)用的Material設(shè)計(jì)中圖片的相關(guān)處理指南
這篇文章主要介紹了Android應(yīng)用的Material設(shè)計(jì)中圖片的相關(guān)處理指南,除了介紹新的方法外文中還給出了一些設(shè)計(jì)標(biāo)準(zhǔn)樣例僅供參考,需要的朋友可以參考下2016-04-04
Android AlertDialog對(duì)話框詳解及實(shí)例
這篇文章主要介紹了Android AlertDialog對(duì)話框詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android 如何保證service在后臺(tái)不被kill
本文主要介紹了Android 如何保證service在后臺(tái)不被kill的方法。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
Android 中TeaPickerView數(shù)據(jù)級(jí)聯(lián)選擇器功能的實(shí)例代碼
這篇文章主要介紹了Android TeaPickerView數(shù)據(jù)級(jí)聯(lián)選擇器 ,需要的朋友可以參考下2019-06-06
Handler制作簡單相冊(cè)查看器的實(shí)例代碼
下面小編就為大家分享一篇Handler制作簡單相冊(cè)查看器的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
關(guān)于Android Device Monitor 無法打開問題
大家好,本篇文章主要講的是關(guān)于Android Device Monitor 無法打開問題,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01

