国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android實現簡易計算功能

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

本文實例為大家分享了Android實現簡易計算功能的具體代碼,供大家參考,具體內容如下

效果如圖:

Android實現簡易計算功能

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='操作數:' android:textSize='20sp' /><EditText android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='1' android:hint='請輸入數值操作數' 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='算術運算:' 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='操作數:' android:textSize='20sp' /><EditText android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='1' android:hint='請輸入數值操作數' 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='計算結果' 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>請選擇運算符</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);//獲取活動布局中的Spinner對象//為Spinner注冊內部監聽器對象spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {//將Spinner選項的值賦值給成員變量op(保存算術運算符)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);//計算核心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://非法情況報錯 result.setText(R.string.ERROR); break; }}); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 精品99视频 | 欧美激情欧美狂野欧美精品免费 | 日韩精品久久久免费观看夜色 | 综合九九 | 国产精品国产三级国产a | 亚洲在线视频播放 | 7777在线| 日韩国产在线观看 | 国产亚洲欧美成人久久片 | 亚洲香蕉久久一区二区 | 久久一区二区精品综合 | 精品一区二区久久久久久久网站 | 免费黄网大全 | 九九久久国产精品 | 成人网18免费软件大全 | 久久精品成人 | 国内精品久久久久久久影视麻豆 | 国产福利社区 | 特级欧美视频aaaaaa | 亚洲精品成人在线 | 国产精品亚洲精品日韩已方 | 国产一区二区fc2ppv在线播放 | 9lporm自拍视频在线 | 岛国在线永久免费视频 | 另类亚洲孕妇分娩网址 | 国产婷婷一区二区在线观看 | a级片一级片 | 99久久精品国产亚洲 | 国产精品三级国语在线看 | 久久久久免费精品国产 | 中文字幕一二三四区2021 | 99视频在线国产 | 午夜a毛片 | 古代级a毛片可以免费看 | 国产一级做a爰片在线看 | 欧美一区二区三区久久综 | 亚洲国产日韩在线 | 香蕉视频黄在线观看 | 国产精品无码久久久久 | 日本亚欧乱色视频在线观看 | 日本三级11k影院在线 |