久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

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

Android實現(xiàn)簡易計算功能

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

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

效果如圖:

Android實現(xiàn)簡易計算功能

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='請輸入數(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ù)運算:' 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='請輸入數(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='計算結(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>請選擇運算符</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注冊內(nèi)部監(jiān)聽器對象spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {//將Spinner選項的值賦值給成員變量op(保存算術(shù)運算符)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; }}); }}

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

標簽: Android
相關文章:
主站蜘蛛池模板: 中文字幕一区在线观看视频 | 91看片在线观看 | 五月婷婷导航 | 精品久久久久久久久久久久久久 | 日本精品一区二区三区视频 | 欧美成人黑人xx视频免费观看 | 91九色在线| 二区在线观看 | 亚洲高清在线观看 | 爱草在线 | 五月激情综合婷婷 | 四虎中文字幕 | 久久99精品久久久久久青青日本 | 精品一区二区久久久久久久网站 | 人人干人人爱 | 久热九九 | yiren22综合网成人 | 亚洲一区二区三区免费 | 色偷偷噜噜噜亚洲男人 | 操操网| 亚洲精品视频在线观看免费视频 | 日韩免费视频 | 青青草免费在线视频 | 成年人黄色一级毛片 | 国产亚洲精品久久久久久豆腐 | 91在线免费观看 | 正在播放国产精品 | 香蕉久久一区二区不卡无毒影院 | 天天天操操操 | 夜夜草天天干 | 亚洲a网| 太子妃好紧皇上好爽h | 欧美二区三区视频 | 午夜亚洲 | 国产成人精品一区一区一区 | 日韩不卡中文字幕 | 欧美国产一区二区三区 | 亚洲午夜视频在线观看 | 中文字幕精品一区久久久久 | 91精品国产综合久久久久久丝袜 | 欧美日韩高清 |