欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開(kāi)發(fā)實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器功能【附完整demo源碼下載】

 更新時(shí)間:2017年11月29日 08:54:36   作者:zml_2015  
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器功能,結(jié)合實(shí)例形式分析了Android計(jì)算器的具體實(shí)現(xiàn)步驟與相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下

本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器功能。分享給大家供大家參考,具體如下:

這個(gè)Android計(jì)算器雖然還有點(diǎn)小bug,不過(guò)簡(jiǎn)單的計(jì)算功能還是沒(méi)問(wèn)題的哦;

先上圖看效果

比較簡(jiǎn)單,所以我就沒(méi)怎么寫(xiě)注釋?zhuān)瑧?yīng)該一看就能明白的
有不明白的可以發(fā)信問(wèn)我

先貼MainActivity.java代碼

package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
  Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bp, bs, bm, bd, bc, be;
  ImageView delete;
  TextView tv;
  EditText show;
  String showString = "", option = "";
  int showfirst = 0;
  String exception = "";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b0 = (Button) findViewById(R.id.bt_0);
    b1 = (Button) findViewById(R.id.bt_1);
    b2 = (Button) findViewById(R.id.bt_2);
    b3 = (Button) findViewById(R.id.bt_3);
    b4 = (Button) findViewById(R.id.bt_4);
    b5 = (Button) findViewById(R.id.bt_5);
    b6 = (Button) findViewById(R.id.bt_6);
    b7 = (Button) findViewById(R.id.bt_7);
    b8 = (Button) findViewById(R.id.bt_8);
    b9 = (Button) findViewById(R.id.bt_9);
    bp = (Button) findViewById(R.id.bt_plus);
    bs = (Button) findViewById(R.id.bt_sub);
    bm = (Button) findViewById(R.id.bt_mutilate);
    bd = (Button) findViewById(R.id.bt_div);
    bc = (Button) findViewById(R.id.bt_c);
    be = (Button) findViewById(R.id.bt_equ);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);
    b4.setOnClickListener(this);
    b5.setOnClickListener(this);
    b6.setOnClickListener(this);
    b7.setOnClickListener(this);
    b8.setOnClickListener(this);
    b9.setOnClickListener(this);
    b0.setOnClickListener(this);
    bp.setOnClickListener(this);
    bs.setOnClickListener(this);
    bm.setOnClickListener(this);
    bd.setOnClickListener(this);
    bc.setOnClickListener(this);
    be.setOnClickListener(this);
    show = (EditText) findViewById(R.id.et_show);
    delete = (ImageView) findViewById(R.id.iv_delete);
    delete.setOnClickListener(this);
    tv=(TextView) findViewById(R.id.author);
    tv.setOnClickListener(this);
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.bt_0:
      showString += "0";
      break;
    case R.id.bt_1:
      showString += "1";
      break;
    case R.id.bt_2:
      showString += "2";
      break;
    case R.id.bt_3:
      showString += "3";
      break;
    case R.id.bt_4:
      showString += "4";
      break;
    case R.id.bt_5:
      showString += "5";
      break;
    case R.id.bt_6:
      showString += "6";
      break;
    case R.id.bt_7:
      showString += "7";
      break;
    case R.id.bt_8:
      showString += "8";
      break;
    case R.id.bt_9:
      showString += "9";
      break;
    case R.id.bt_plus:
      if (showString.equals(""))
        exception = "先輸入數(shù)值哦";
      else {
        showfirst = Integer.parseInt(showString);
        showString = "";
        option = "+";
      }
      break;
    case R.id.bt_sub:
      if (showString.equals(""))
        exception = "先輸入數(shù)值哦";
      else {
        showfirst = Integer.parseInt(showString);
        showString = "";
        option = "-";
      }
      break;
    case R.id.bt_mutilate:
      if (showString.equals(""))
        exception = "先輸入數(shù)值哦";
      else {
        showfirst = Integer.parseInt(showString);
        showString = "";
        option = "*";
      }
      break;
    case R.id.bt_div:
      if (showString.equals(""))
        exception = "先輸入數(shù)值哦";
      else {
        showfirst = Integer.parseInt(showString);
        showString = "";
        option = "/";
      }
      break;
    case R.id.bt_equ:
      if (option.equals("+"))
        showString = showfirst + Integer.parseInt(showString) + "";
      else if (option.equals("-")) {
        showString = showfirst - Integer.parseInt(showString) + "";
      } else if (option.equals("*")) {
        showString = showfirst * Integer.parseInt(showString) + "";
      } else if (option.equals("/")) {
        if (showString.equals("0")) {
          exception = "除數(shù)不能為0!";
        } else
          showString = showfirst / Integer.parseInt(showString) + "";
      }
      break;
    case R.id.bt_c:
      showString = "";
      break;
    case R.id.iv_delete:
      Toast.makeText(MainActivity.this, showString + "已被清空",
          Toast.LENGTH_SHORT).show();
      showString = "";
      break;
    case R.id.author:
      Toast.makeText(MainActivity.this, "鄭明亮\n軟件工程\nQQ:1072307340",
          Toast.LENGTH_SHORT).show();
      break;
    default:
      break;
    }
    if (exception.equals(""))
      show.setText(showString);
    else {
      show.setText(exception);
      exception = "";
    }
    // 設(shè)置文本框顏色;
    if (!show.getText().toString().equals("")) {
      delete.setBackgroundColor(R.drawable.delete_gray);
    }
    else {
      delete.setBackgroundResource(R.drawable.delete);
    }
  }
}

再貼布局activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.example.calculator.MainActivity" >
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <EditText
      android:id="@+id/et_show"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:hint="請(qǐng)輸入數(shù)字" />
    <ImageView
      android:id="@+id/iv_delete"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@id/et_show"
      android:src="@drawable/delete_and_deletegray" >
    </ImageView>
  </RelativeLayout>
  <GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et_show"
    android:focusable="false"
    android:gravity="center"
    android:layout_marginTop="25dp"
    android:columnCount="4"
    android:horizontalSpacing="0dp"
    android:orientation="horizontal"
    android:stretchMode="none" >
    <Button
      android:id="@+id/bt_1"
      android:layout_height="wrap_content"
      android:text="1" />
    <Button
      android:id="@+id/bt_2"
      android:text="2" />
    <Button
      android:id="@+id/bt_3"
      android:text="3" />
    <Button
      android:id="@+id/bt_div"
      android:text="/" />
    <Button
      android:id="@+id/bt_4"
      android:text="4" />
    <Button
      android:id="@+id/bt_5"
      android:text="5" />
    <Button
      android:id="@+id/bt_6"
      android:text="6" />
    <Button
      android:id="@+id/bt_mutilate"
      android:text="X" />
    <Button
      android:id="@+id/bt_7"
      android:text="7" />
    <Button
      android:id="@+id/bt_8"
      android:text="8" />
    <Button
      android:id="@+id/bt_9"
      android:text="9" />
    <Button
      android:id="@+id/bt_sub"
      android:text="-" />
    <Button
      android:id="@+id/bt_0"
      android:layout_columnSpan="2"
      android:layout_gravity="fill_horizontal"
      android:text="0"
      android:width="2dp" />
    <Button
      android:id="@+id/bt_c"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="C" />
    <Button
      android:id="@+id/bt_plus"
      android:layout_gravity="fill_vertical"
      android:layout_rowSpan="2"
      android:text="+" />
    <Button
      android:id="@+id/bt_equ"
      android:layout_columnSpan="3"
      android:layout_gravity="fill_horizontal"
      android:text="=" />
  </GridLayout>
  <TextView
    android:id="@+id/author"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Author:Bri"
    />
  <TextView
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="test:www.dbjr.com.cn"
    />
</LinearLayout>

我還寫(xiě)了一個(gè)drawable的xml,自己看吧

delete_and_deletegray.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/delete_gray" android:state_focused="false" android:state_pressed="false"></item>
  <item android:drawable="@drawable/delete" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
  <item android:drawable="@drawable/delete" android:state_pressed="true" android:state_selected="false"/>
  <item android:drawable="@drawable/delete" android:state_focused="true" android:state_pressed="true"/>
</selector>

附:完整實(shí)例代碼點(diǎn)擊此處本站下載。

PS:這里再為大家推薦幾款計(jì)算工具供大家進(jìn)一步參考借鑒:

在線一元函數(shù)(方程)求解計(jì)算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

科學(xué)計(jì)算器在線使用_高級(jí)計(jì)算器在線計(jì)算:
http://tools.jb51.net/jisuanqi/jsqkexue

在線計(jì)算器_標(biāo)準(zhǔn)計(jì)算器:
http://tools.jb51.net/jisuanqi/jsq

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android自定義實(shí)現(xiàn)圖片加文字功能

    Android自定義實(shí)現(xiàn)圖片加文字功能

    這篇文章主要介紹了Android自定義實(shí)現(xiàn)圖片加文字功能的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 解決Android SELinux權(quán)限問(wèn)題記錄分析

    解決Android SELinux權(quán)限問(wèn)題記錄分析

    這篇文章主要為大家介紹了解決Android SELinux權(quán)限問(wèn)題記錄分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Android Socket通信詳解

    Android Socket通信詳解

    這篇文章主要介紹了Android Socket通信詳解的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • Android 8.0中一些坑以及對(duì)應(yīng)的解決方法

    Android 8.0中一些坑以及對(duì)應(yīng)的解決方法

    這篇文章主要給大家介紹了關(guān)于Android 8.0中一些坑以及對(duì)應(yīng)的解決方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • 使用Flutter開(kāi)發(fā)一個(gè)圖片UI組件的代碼示例

    使用Flutter開(kāi)發(fā)一個(gè)圖片UI組件的代碼示例

    在移動(dòng)應(yīng)用開(kāi)發(fā)中,圖片展示是一個(gè)常見(jiàn)的需求,為了滿足不同場(chǎng)景的圖片展示需求,我們可以開(kāi)發(fā)一個(gè)靈活配置的圖片UI組件,本文將介紹如何使用Flutter開(kāi)發(fā)一個(gè)圖片UI組件,并提供了豐富的配置選項(xiàng),需要的朋友可以參考下
    2023-09-09
  • Android studio 項(xiàng)目手動(dòng)在本地磁盤(pán)中刪除module后,殘留文件夾無(wú)法刪除的問(wèn)題解決方法

    Android studio 項(xiàng)目手動(dòng)在本地磁盤(pán)中刪除module后,殘留文件夾無(wú)法刪除的問(wèn)題解決方法

    這篇文章主要介紹了Android studio 項(xiàng)目手動(dòng)在本地磁盤(pán)中刪除module后,殘留文件夾無(wú)法刪除問(wèn)題,本文給出了解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Android自定義格式顯示Button的布局思路

    Android自定義格式顯示Button的布局思路

    下文的效果都是xml布局文件實(shí)現(xiàn)的,一張圖片都未曾使用,順便貼出幾個(gè)布局文件留個(gè)大家參考下,感性的朋友可不要錯(cuò)過(guò)了哈
    2013-04-04
  • Kotlin?Navigation可視化開(kāi)發(fā)詳解

    Kotlin?Navigation可視化開(kāi)發(fā)詳解

    Navigation?是?JetPack?中的一個(gè)組件,用于方便的實(shí)現(xiàn)頁(yè)面的導(dǎo)航,所以抽象出了一個(gè)?destination?的概念,大部分情況一個(gè)?destination?就表示一個(gè)?Fragment,但是它同樣可以指代?Activity、其它的導(dǎo)航圖
    2023-02-02
  • Android PopupWindow使用方法小結(jié)

    Android PopupWindow使用方法小結(jié)

    這篇文章主要為大家詳細(xì)介紹了Android PopupWindow使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • android自定義簡(jiǎn)單時(shí)鐘

    android自定義簡(jiǎn)單時(shí)鐘

    這篇文章主要為大家詳細(xì)介紹了android自定義簡(jiǎn)單時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03

最新評(píng)論