色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:8日期:2022-09-17 18:24:00

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

效果如圖:

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

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'> <LinearLayoutandroid: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: 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> <LinearLayoutandroid: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: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='1' android:entries='@array/sign' /> </LinearLayout> <LinearLayoutandroid: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: 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> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'><Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='1' android:text='等于:' /><TextView android: 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; }}); }}

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

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 一色屋色费精品视频在线观看 | 日本天堂视频在线观看 | 加勒比综合网 | 久久久久国产精品 | 国产一级aa大片毛片 | 国产精品国产亚洲精品不卡 | 国产三级在线 | 久久精品一区二区三区四区 | 九九精品免视看国产成人 | 国产成人精品视频一区 | 最新国产精品自拍 | 国产精选91热在线观看 | 大尺度福利视频奶水在线 | 国产精品久久久久激情影院 | 一级特级欧美a毛片免费 | 草草草在线视频 | 国产精品国产三级在线高清观看 | 啪啪一级片 | 91大神大战丝袜美女在线观看 | 成人高清在线观看播放 | 狠狠色狠狠综合久久 | 国产一区二区精品在线观看 | 国产中文字幕在线播放 | 欧美人与zoxxxx另类9 | 久久国产三级精品 | 另类视频区第一页 | 国产大陆精品另类xxxx | 亚洲在线免费视频 | 日韩美女免费视频 | 男女视频在线观看免费高清观看 | 我要看欧美精品一级毛片 | 欧美xxx在线 | 精品亚洲一区二区 | 国产免费高清福利拍拍拍 | 美国毛片毛片全部免费 | 久久亚洲精品中文字幕第一区 | 日韩不卡一级毛片免费 | 欧毛片 | 亚洲综合伦理一区 | 一区二区三区日韩 | 中文字幕一区在线播放 |