最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算功能

 更新時(shí)間:2021年06月22日 08:40:33   作者:青絲纏光陰  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡(jiǎ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)聽(tīng)器對(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) {
            }
        });

        //各類(lèi)組件
        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;
            }
        });
    }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 10 適配攻略小結(jié)

    Android 10 適配攻略小結(jié)

    這篇文章主要介紹了Android 10 適配攻略小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Android提高之MediaPlayer音視頻播放

    Android提高之MediaPlayer音視頻播放

    這篇文章主要介紹了Android提高之MediaPlayer音視頻播放,很實(shí)用的功能,需要的朋友可以參考下
    2014-08-08
  • Android RecyclerView 基礎(chǔ)知識(shí)詳解

    Android RecyclerView 基礎(chǔ)知識(shí)詳解

    本文主要介紹Android RecyclerView的資料,這里對(duì)RecyclerView 的基礎(chǔ)知識(shí)做了詳細(xì)講解,并附簡(jiǎn)單示例代碼幫助大家學(xué)習(xí)參考,有需要的小伙伴可以參考下
    2016-09-09
  • Android實(shí)現(xiàn)讀取掃碼槍內(nèi)容(條形碼)

    Android實(shí)現(xiàn)讀取掃碼槍內(nèi)容(條形碼)

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)讀取掃碼槍內(nèi)容、條形碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 安裝時(shí)加入外部數(shù)據(jù)庫(kù)示例(android外部數(shù)據(jù)庫(kù))

    安裝時(shí)加入外部數(shù)據(jù)庫(kù)示例(android外部數(shù)據(jù)庫(kù))

    這篇文章主要介紹了android打包安裝時(shí)加入外部數(shù)據(jù)庫(kù)的示例,需要的朋友可以參考下
    2014-03-03
  • Android實(shí)現(xiàn)輪播圖片展示效果

    Android實(shí)現(xiàn)輪播圖片展示效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)輪播圖片展示效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • android實(shí)現(xiàn)圓形漸變進(jìn)度條

    android實(shí)現(xiàn)圓形漸變進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)圓形漸變進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • Android實(shí)現(xiàn)3D標(biāo)簽云效果

    Android實(shí)現(xiàn)3D標(biāo)簽云效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)3D標(biāo)簽云效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 詳解Android中Drawable方法

    詳解Android中Drawable方法

    這篇文章主要為大家詳細(xì)介紹了Android中Drawable方法,感興趣的朋友可以參考一下
    2016-05-05
  • 怎么發(fā)布打包并發(fā)布自己的Android應(yīng)用(APP)

    怎么發(fā)布打包并發(fā)布自己的Android應(yīng)用(APP)

    前面我為大家講的都是關(guān)于Android開(kāi)發(fā)方面的知識(shí)點(diǎn)和技術(shù),不少朋友可能會(huì)感到疑惑--究竟我該怎么打包、發(fā)布自己開(kāi)發(fā)的APP,怎樣將我的APP放到網(wǎng)上工別人下載,怎樣保證我的APP安全及版權(quán)問(wèn)題呢
    2013-11-11

最新評(píng)論

古丈县| 陵水| 独山县| 马山县| 天峻县| 德惠市| 霍州市| 莫力| 定州市| 普兰县| 漳浦县| 阳新县| 云龙县| 垦利县| 黄山市| 都江堰市| 台北县| 北海市| 突泉县| 康马县| 温州市| 勐海县| 新密市| 盐津县| 集安市| 怀仁县| 龙州县| 陵水| 军事| 修水县| 浮梁县| 钟祥市| 双流县| 广昌县| 德化县| 卢湾区| 辽源市| 东光县| 玛沁县| 阿克陶县| 咸丰县|