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

android簡易計算器的制作

 更新時間:2020年07月29日 10:37:58   作者:Timber666  
這篇文章主要為大家詳細介紹了android簡易計算器的制作,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

之前有好好完成老師留過的C++大作業(yè),使用MFC制作通訊錄。所以用AS寫一個安卓的計算器并不是很難,但還是想上手操作一下,寫一個只有簡單加減乘除運算的小計算器,后面可能會考慮加一些其他的稍微復(fù)雜的計算功能。下面是步驟。

1.首先創(chuàng)建一個empty activity,取名為MyStudyCalculator。

2.打開activity_main.xml文件,創(chuàng)建兩個編輯框(EditText)、四個按鈕(Button)、一個文本框(TextView),并設(shè)置相應(yīng)的id。其中編輯框作用是讓用戶填入兩個數(shù)字,四個按鈕分別對應(yīng)四種不同的運算(需要對按鈕分別添加響應(yīng)事件),文本框用于顯示運算結(jié)果。我另外添加了兩個文本框,一個用于顯示標題,一個顯示作者,對于該計算器來說沒有任何作用。下面給出代碼:

//第一個數(shù)字
  <EditText
    android:id="@+id/first"
    android:layout_width="85dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="56dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="168dp"
    android:hint="@string/num1"
    app:layout_constraintEnd_toStartOf="@+id/second"
    app:layout_constraintHorizontal_bias="0.621"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  //第二個數(shù)字
  <EditText
    android:id="@+id/second"
    android:layout_width="85dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="64dp"
    android:layout_marginTop="168dp"
    android:hint="@string/num2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  //第二個數(shù)字
  <TextView
    android:id="@+id/res"
    android:layout_width="96dp"
    android:layout_height="33dp"
    android:layout_marginBottom="84dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:gravity="center"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.481"
    app:layout_constraintStart_toStartOf="parent" />
 
  //加法按鈕
  <Button
    android:id="@+id/button1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="260dp"
    android:onClick="SUM"
    android:text="+"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.391"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  //減法按鈕
  <Button
    android:id="@+id/button2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="30dp"
    android:layout_marginEnd="148dp"
    android:layout_marginTop="8dp"
    android:onClick="SUB"
    android:text="-"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.595" />
 
  //乘法按鈕
  <Button
    android:id="@+id/button3"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="152dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:onClick="MUL"
    android:text="*"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.393"
    app:layout_constraintStart_toStartOf="parent" />
 
  //除法按鈕
  <Button
    android:id="@+id/button4"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="148dp"
    android:layout_marginTop="8dp"
    android:onClick="DIV"
    android:text="/"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.678" />
 
  //標題
  <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="36dp"
    android:text="丑陋的而且只能算加減乘除的計算機"
    android:textSize="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
 
  //作者
  <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="11dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="496dp"
    android:text="TimberWolf"
    android:textSize="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.99"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

3.打開MainActivity.java寫四個按鈕對應(yīng)的方法,代碼如下:

//加法
  public void SUM(View view) {
    EditText first = (EditText)findViewById(R.id.first);
    EditText second = (EditText)findViewById(R.id.second);
    TextView res = (TextView)findViewById(R.id.res);
    double num1 = 0;
    double num2 = 0;
    double ans = 0;
    String numfirst = first.getText().toString();
    String numsecond = second.getText().toString();
    num1 = Double.parseDouble(numfirst);
    num2 = Double.parseDouble(numsecond);
    ans = num1 + num2;
    res.setText(String.valueOf(ans));
  }
  //減法
  public void SUB(View view) {
    EditText first = (EditText)findViewById(R.id.first);
    EditText second = (EditText)findViewById(R.id.second);
    TextView res = (TextView)findViewById(R.id.res);
    double num1 = 0;
    double num2 = 0;
    double ans = 0;
    String numfirst = first.getText().toString();
    String numsecond = second.getText().toString();
    num1 = Double.parseDouble(numfirst);
    num2 = Double.parseDouble(numsecond);
    ans = num1 - num2;
    res.setText(String.valueOf(ans));
  }
  //乘法
  public void MUL(View view) {
    EditText first = (EditText)findViewById(R.id.first);
    EditText second = (EditText)findViewById(R.id.second);
    TextView res = (TextView)findViewById(R.id.res);
    double num1 = 0;
    double num2 = 0;
    double ans = 0;
    String numfirst = first.getText().toString();
    String numsecond = second.getText().toString();
    num1 = Double.parseDouble(numfirst);
    num2 = Double.parseDouble(numsecond);
    ans = num1 * num2;
    res.setText(String.valueOf(ans));
  }
  //除法
  public void DIV(View view) {
    EditText first = (EditText)findViewById(R.id.first);
    EditText second = (EditText)findViewById(R.id.second);
    TextView res = (TextView)findViewById(R.id.res);
    double num1 = 0;
    double num2 = 0;
    double ans = 0;
    String numfirst = first.getText().toString();
    String numsecond = second.getText().toString();
    num1 = Double.parseDouble(numfirst);
    num2 = Double.parseDouble(numsecond);
    ans = num1 / num2;
    res.setText(String.valueOf(ans));
  }

4.看似代碼很長,其實都是一樣的,很簡單就完成了,其中MainActivity.java中的代碼我沒有給出注釋,就是幾種類型的轉(zhuǎn)換。歡迎大佬指點,初學(xué)者不懂的可以給我留言。下面給出仿真機運行效果。

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

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

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

相關(guān)文章

  • Android實現(xiàn)輪播圖無限循環(huán)效果

    Android實現(xiàn)輪播圖無限循環(huán)效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)輪播圖無限循環(huán)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • FragmentTabHost使用方法詳解

    FragmentTabHost使用方法詳解

    這篇文章主要為大家詳細介紹了Android中FragmentTabHost的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Kotlin中雙冒號::使用方法

    Kotlin中雙冒號::使用方法

    這篇文章主要給大家介紹了關(guān)于Kotlin中雙冒號::使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Kotlin具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • android使用PullToRefresh實現(xiàn)下拉刷新和上拉加載

    android使用PullToRefresh實現(xiàn)下拉刷新和上拉加載

    本篇文章主要介紹了android使用PullToRefresh實現(xiàn)下拉刷新和上拉加載,具有一定的參考價值,有興趣的可以了解一下。
    2016-12-12
  • 常用Android布局文件優(yōu)化技巧總結(jié)

    常用Android布局文件優(yōu)化技巧總結(jié)

    Android布局加載是Android應(yīng)用程序的重要組成部分,布局加載是指將 XML文件中定義的視圖層次結(jié)構(gòu)加載到內(nèi)存中,在這篇文章中,我們將深入探討 Android 布局加載的原理,包括 Android 布局文件的結(jié)構(gòu)和布局文件的常見問題等方面,需要的朋友可以參考下
    2023-07-07
  • 頁面未隨軟鍵盤上升及android隱藏軟鍵盤總結(jié)

    頁面未隨軟鍵盤上升及android隱藏軟鍵盤總結(jié)

    這篇文章主要介紹了頁面未隨軟鍵盤上升及android隱藏軟鍵盤總結(jié),需要的朋友可以參考下
    2015-12-12
  • MacBook M1 Flutter環(huán)境搭建的實現(xiàn)步驟

    MacBook M1 Flutter環(huán)境搭建的實現(xiàn)步驟

    本文主要介紹了MacBook M1 Flutter環(huán)境搭建,F(xiàn)lutter官方和各項配套的軟件環(huán)境也還沒有成熟,導(dǎo)致搭建環(huán)境時碰到了不少坑,本文就詳細的介紹一下
    2021-08-08
  • Android中檢查、設(shè)置默認程序詳解

    Android中檢查、設(shè)置默認程序詳解

    這篇文章主要介紹了Android中檢查、設(shè)置默認程序詳解,本文講解了檢測是否有默認的程序、如果有默認程序、沒有默認的程序的情況等內(nèi)容,需要的朋友可以參考下
    2015-01-01
  • Android 自定義九宮格手勢鎖

    Android 自定義九宮格手勢鎖

    本文通過實例代碼給大家介紹了android自定義九宮格手勢鎖功能,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧
    2017-06-06
  • Compose自定義View實現(xiàn)宇智波斑寫輪眼

    Compose自定義View實現(xiàn)宇智波斑寫輪眼

    這篇文章主要為大家介紹了Compose自定義View實現(xiàn)宇智波斑寫輪眼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02

最新評論

哈巴河县| 娄底市| 明溪县| 梨树县| 莱西市| 东乌珠穆沁旗| 郓城县| 蒲江县| 乌鲁木齐市| 蓬莱市| 靖远县| 体育| 丰原市| 弥渡县| 弥渡县| 鄂托克旗| 涿州市| 三原县| 堆龙德庆县| 马公市| 兴安县| 乌拉特后旗| 内黄县| 青海省| 巢湖市| 涟源市| 黔西县| 岢岚县| 沈阳市| 游戏| 平定县| 嘉定区| 邵阳县| 昭通市| 宣武区| 广河县| 永仁县| 沁阳市| 玛多县| 方城县| 应用必备|