av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

Android實現(xiàn)加法計算器

瀏覽:120日期:2022-09-25 10:53:37

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

Android實現(xiàn)加法計算器

Android實現(xiàn)加法計算器

Android實現(xiàn)加法計算器

布局

<?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:orientation='vertical'> <!-- 加法器名稱--> <TextView android:layout_width='match_parent' android:layout_height='wrap_content' android:text='加法器' android:textSize='40sp' android:textColor='#E6BD1A' android:gravity='center' android:layout_marginTop='20sp' android:textStyle='bold'/> <!-- 第1個數(shù)字輸入框 --> <EditText android: /> <!-- 加法符號 --> <TextView android:layout_width='match_parent' android:layout_height='wrap_content' android:text='+' android:textStyle='bold' android:textSize='40sp' android:textColor='#1A94E6' android:gravity='center' android:layout_marginTop='50sp'/> <!-- 第2個數(shù)字輸入框--> <EditText android: /> <!-- 等于和歸零符號 --> <LinearLayout android:layout_width='wrap_content' android:layout_height='wrap_content' android:orientation='horizontal' android:layout_gravity='center' android:layout_marginTop='20sp' android:layout_marginBottom='20sp'> <!-- 等于符號 --> <Button android: android:text='=' android:layout_marginRight='20sp'/> <!-- 歸零符號 --> <Button android: android:text='C'/> </LinearLayout> <!-- 結(jié)果輸入框 --> <EditText android: android:enabled='false'/></LinearLayout>

樣式

<resources> <!-- Base application theme. --> <style name='AppTheme' parent='Theme.AppCompat.Light.DarkActionBar'> <!-- Customize your theme here. --> <item name='colorPrimary'>@color/colorPrimary</item> <item name='colorPrimaryDark'>@color/colorPrimaryDark</item> <item name='colorAccent'>@color/colorAccent</item> </style> <!-- 3個輸入框的樣式 --> <style name='input_style'> <item name='android:layout_gravity'>center</item> <item name='android:gravity'>center</item> <item name='android:layout_width'>350sp</item> <item name='android:layout_height'>wrap_content</item> <item name='android:color'>#000000</item> <item name='android:layout_marginTop'>15sp</item> <item name='android:inputType'>number</item> </style> <!-- 等于和歸零按鈕樣式 --> <style name='button_style'> <item name='android:layout_width'>wrap_content</item> <item name='android:layout_height'>wrap_content</item> <item name='android:textSize'>30sp</item> <item name='android:gravity'>center</item> </style></resources>

java代碼

package cn.itcast.totalizator;import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity { //輸入框input1 input2 // 結(jié)果框input3 private EditText input1 = null; private EditText input2 = null; private EditText input3 = null; //等于按鈕btn1 //歸零按鈕btn2 private Button btn1 = null; private Button btn2 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); input1 = (EditText) findViewById(R.id.input1); input2 = (EditText) findViewById(R.id.input2); input3 = (EditText) findViewById(R.id.input3); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); //點擊等于按鈕觸發(fā)監(jiān)聽事件 btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String input1Str = input1.getText().toString(); String input2Str = input2.getText().toString(); if (! input1Str.equals('') && ! input2Str.equals('')) { double number1 = Double.parseDouble(input1Str); double number2 = Double.parseDouble(input2Str); double result = number1 + number2; input3.setText(result + ''); } else{ Toast.makeText(getApplicationContext(),'請輸入合法的數(shù)字',Toast.LENGTH_SHORT).show(); } } }); //點擊歸零按鈕觸發(fā)監(jiān)聽事件 btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //把三個輸入框都設(shè)置為空 input1.setText(''); input2.setText(''); input3.setText(''); } }); } //點擊退出按鈕,彈出是否確認退出對話框 @Override public void onBackPressed() { //聲明對象 AlertDialog dialog; //綁定當前界面窗口,設(shè)置標題 dialog = new AlertDialog.Builder(this) .setTitle('Dialog對話框') .setMessage('是否確定退出') .setIcon(R.mipmap.ic_launcher) .setPositiveButton('確定',listener) .setNegativeButton('取消', listener) .create(); dialog.show(); } //監(jiān)聽對話框里面的button點擊事件 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which) { case AlertDialog.BUTTON_POSITIVE:// '確認'按鈕退出程序 finish(); break; case AlertDialog.BUTTON_NEGATIVE:// '取消'第二個按鈕取消對話框 break; default: break; } } };}

更多計算器功能實現(xiàn),請點擊專題: 計算器功能匯總 進行學(xué)習(xí)

關(guān)于Android計算器功能的實現(xiàn),查看專題:Android計算器 進行學(xué)習(xí)。

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

標簽: Android
相關(guān)文章:
主站蜘蛛池模板: 日本三级线观看 视频 | 久久久影院 | 一区二区三区精品 | 国产一区二区在线免费观看 | 国产精品久久久久久久久久久久久久 | 国产欧美一级二级三级在线视频 | 中文字幕成人在线 | 人人做人人澡人人爽欧美 | 久久性色 | 成人免费在线 | 精品人伦一区二区三区蜜桃网站 | 男人的天堂在线视频 | 久久美女网 | 91精品国产综合久久久久 | 国产日韩一区二区 | 亚洲欧美在线视频 | 亚洲精品资源 | 精品不卡 | 日韩欧美国产精品一区二区 | 国产精品一区二区久久久久 | 婷婷午夜天 | 国产精品欧美一区二区三区不卡 | 久久精品久久综合 | 国产精品婷婷 | 久久中文高清 | 久久久91| 久久成人一区 | 日日夜夜精品视频 | 羞羞涩涩在线观看 | 久久伊人一区 | 97超碰免费 | 久久久久国产一区二区三区 | 精品福利在线 | 亚洲精品99 | 日日日色 | 国产成人99久久亚洲综合精品 | 日韩欧美专区 | 日本大香伊一区二区三区 | 伊人网在线看 | 国产精品久久久久久久久久久久久久 | 亚洲一区二区三区在线播放 |