亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能

 更新時(shí)間:2020年05月22日 16:54:26   作者:蕭丶RY  
這篇文章主要為大家詳細(xì)介紹了android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android studio實(shí)現(xiàn)計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下

先來(lái)個(gè)效果圖:

功能: 滿足加減乘除四則運(yùn)算規(guī)則,有回退、清除功能。

下面的代碼只是完成基本功能,若添加背景圖先看看下面的方法:Android Studio App設(shè)置背景圖片

1、本地準(zhǔn)備好圖片,復(fù)制它,粘貼進(jìn)mipmap(drawable)文件夾。

2、在activity_main.xml里添加下面代碼,注意添加位置。

3、完成,效果圖:

content_main.xml文件(頁(yè)面布局,content_main.xml代碼包含在activity_main.xml文件中):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/next"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 app:layout_behavior="@string/appbar_scrolling_view_behavior"
 tools:context=".MainActivity"
 tools:showIn="@layout/activity_main">

 <!--<TextView-->
 <!--android:layout_width="wrap_content"-->
 <!--android:layout_height="wrap_content"-->
 <!--android:text="Hello World!"-->
 <!--app:layout_constraintBottom_toBottomOf="parent"-->
 <!--app:layout_constraintLeft_toLeftOf="parent"-->
 <!--app:layout_constraintRight_toRightOf="parent"-->
 <!--app:layout_constraintTop_toTopOf="parent" />-->


 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >


 <EditText
 android:id="@+id/input"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:editable="false"
 android:hint="@string/shuru" />

 <EditText
 android:id="@+id/output"
 android:layout_width="match_parent"
 android:layout_height="70dp"
 android:layout_gravity="center"
 android:editable="true"
 android:gravity="right"
 android:hint="@string/shuchu" />

 <RelativeLayout
 android:layout_width="350dp"
 android:layout_height="wrap_content"
 android:layout_gravity="center">

 <Button
  android:id="@+id/clear"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:text="@string/clear"
  android:textSize="40sp" />

 <Button
  android:id="@+id/back"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/clear"
  android:text="@string/back"
  android:textSize="40sp" />

 <Button
  android:id="@+id/divide"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/back"
  android:text="@string/divide"
  android:textSize="40sp" />

 <Button
  android:id="@+id/multiply"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/divide"
  android:text="@string/multiply"
  android:textSize="40sp" />

 <Button
  android:id="@+id/seven"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_below="@id/clear"
  android:text="@string/seven"
  android:textSize="40sp" />

 <Button
  android:id="@+id/eight"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@id/seven"
  android:layout_below="@id/divide"
  android:text="@string/eight"
  android:textSize="40sp" />

 <Button
  android:id="@+id/nine"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/multiply"
  android:layout_toRightOf="@id/eight"
  android:text="@string/nine"
  android:textSize="40sp" />

 <Button
  android:id="@+id/subtract"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_below="@id/multiply"
  android:layout_toRightOf="@id/nine"
  android:text="@string/subtract"
  android:textSize="40sp" />

 <Button
  android:id="@+id/four"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_below="@id/seven"
  android:text="@string/four"
  android:textSize="40sp" />

 <Button
  android:id="@+id/five"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/eight"
  android:layout_toRightOf="@id/four"
  android:text="@string/five"
  android:textSize="40sp" />

 <Button
  android:id="@+id/six"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/nine"
  android:layout_toRightOf="@id/five"
  android:text="@string/six"
  android:textSize="40sp" />

 <Button
  android:id="@+id/add"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_below="@id/subtract"
  android:layout_toRightOf="@id/six"
  android:text="@string/add"
  android:textSize="40sp" />

 <Button
  android:id="@+id/one"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"

  android:layout_below="@id/four"
  android:text="@string/one"
  android:textSize="40sp" />

 <Button
  android:id="@+id/two"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/five"
  android:layout_toRightOf="@id/one"
  android:text="@string/two"
  android:textSize="40sp" />

 <Button
  android:id="@+id/three"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/six"
  android:layout_toRightOf="@id/two"
  android:text="@string/three"
  android:textSize="40sp" />

 <Button
  android:id="@+id/result"
  android:layout_width="wrap_content"
  android:layout_height="146dp"
  android:layout_alignParentRight="true"
  android:layout_below="@id/add"
  android:layout_toRightOf="@id/three"
  android:text="@string/result"
  android:textSize="40sp" />


 <Button
  android:id="@+id/zero"
  android:layout_width="175dp"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_below="@id/one"
  android:text="@string/zero"
  android:textSize="40sp" />

 <Button
  android:id="@+id/dot"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@id/three"
  android:layout_toRightOf="@id/zero"
  android:text="@string/dot"
  android:textSize="40sp" />

 </RelativeLayout>

 </LinearLayout>


</android.support.constraint.ConstraintLayout>

strings.xml(content_main.xml代碼中的一些變量在此代碼中定義的):

<resources>
 <string name="app_name">Calculator</string>
 <string name="action_settings">Settings</string>
 <string name="title_activity_page2">page2</string>

 <string name="next">下一頁(yè)</string>
 <string name="zero">0</string>
 <string name="one">1</string>
 <string name="two">2</string>
 <string name="three">3</string>
 <string name="four">4</string>
 <string name="five">5</string>
 <string name="six">6</string>
 <string name="seven">7</string>
 <string name="eight">8</string>
 <string name="nine">9</string>
 <string name="add">+</string>
 <string name="subtract">-</string>
 <string name="multiply">*</string>
 <string name="divide">/</string>
 <string name="clear">CE</string>
 <string name="back">&#60;-</string>
 <string name="result">=</string>
 <string name="shuru">請(qǐng)輸入:</string>
 <string name="shuchu">結(jié)果:</string>
 <string name="dot">.</string>
 <string name="resultText">計(jì)算式</string>

</resources>

MainActivity.Java(計(jì)算器中實(shí)現(xiàn)計(jì)算功能的核心代碼):

package com.example.dell.calculator;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import android.app.Activity;
import android.content.Context;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Button;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MainActivity extends AppCompatActivity {

 private EditText output = null;
 private EditText input = null;
 private Button btn0 = null;
 private Button btn1 = null;
 private Button btn2 = null;
 private Button btn3 = null;
 private Button btn4 = null;
 private Button btn5 = null;
 private Button btn6 = null;
 private Button btn7 = null;
 private Button btn8 = null;
 private Button btn9 = null;
 private Button btnadd = null;
 private Button btnsubtract = null;
 private Button btnmultiply = null;
 private Button btndivide = null;
 private Button btnclear = null;
 private Button btnback = null;
 private Button btndot = null;
 private Button btnresult = null;

 private String text = "";//保存輸入的數(shù)字和符號(hào)
 private Double result = 0.0;//輸出結(jié)果

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

 output = (EditText) findViewById(R.id.output);
 input = (EditText) findViewById(R.id.input);

 btn0 = (Button) findViewById(R.id.zero);
 btn1 = (Button) findViewById(R.id.one);
 btn2 = (Button) findViewById(R.id.two);
 btn3 = (Button) findViewById(R.id.three);
 btn4 = (Button) findViewById(R.id.four);
 btn5 = (Button) findViewById(R.id.five);
 btn6 = (Button) findViewById(R.id.six);
 btn7 = (Button) findViewById(R.id.seven);
 btn8 = (Button) findViewById(R.id.eight);
 btn9 = (Button) findViewById(R.id.nine);
 btnadd = (Button) findViewById(R.id.add);
 btnsubtract = (Button) findViewById(R.id.subtract);
 btnmultiply = (Button) findViewById(R.id.multiply);
 btndivide = (Button) findViewById(R.id.divide);
 btnclear = (Button) findViewById(R.id.clear);
 btnback = (Button) findViewById(R.id.back);
 btndot = (Button) findViewById(R.id.dot);
 btnresult = (Button) findViewById(R.id.result);

 //設(shè)置按鈕偵聽(tīng)事件
 btn0.setOnClickListener(listener);
 btn1.setOnClickListener(listener);
 btn2.setOnClickListener(listener);
 btn3.setOnClickListener(listener);
 btn4.setOnClickListener(listener);
 btn5.setOnClickListener(listener);
 btn6.setOnClickListener(listener);
 btn7.setOnClickListener(listener);
 btn8.setOnClickListener(listener);
 btn9.setOnClickListener(listener);

 btnadd.setOnClickListener(listener);
 btnsubtract.setOnClickListener(listener);
 btnmultiply.setOnClickListener(listener);
 btndivide.setOnClickListener(listener);
 btnclear.setOnClickListener(listener);
 btnback.setOnClickListener(listener);
 btndot.setOnClickListener(listener);
 btnresult.setOnClickListener(listener);


 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
 fab.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
  Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
   .setAction("Action", null).show();
  }
 });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.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();

 //noinspection SimplifiableIfStatement
 if (id == R.id.action_settings) {
  return true;
 }

 return super.onOptionsItemSelected(item);
 }


 //public void onClickNext(View view) {
 // Intent intent = new Intent(this,page2.class);
 // startActivity(intent);
 // }


 private OnClickListener listener = new OnClickListener() {

 public void onClick(View v) {
  // TODO Auto-generated method stub
  switch (v.getId()) {
  //輸入數(shù)字
  case R.id.zero:
   num(0);
   break;
  case R.id.one:
   num(1);
   break;
  case R.id.two:
   num(2);
   break;
  case R.id.three:
   num(3);
   break;
  case R.id.four:
   num(4);
   break;
  case R.id.five:
   num(5);
   break;
  case R.id.six:
   num(6);
   break;
  case R.id.seven:
   num(7);
   break;
  case R.id.eight:
   num(8);
   break;
  case R.id.nine:
   num(9);
   break;

  case R.id.dot:
  dot();
  break;
  //執(zhí)行運(yùn)算
  case R.id.add:
   add();
   break;
  case R.id.subtract:
   sub();
   break;
  case R.id.multiply:
   multiply();
   break;
  case R.id.divide:
   divide();
   break;
  case R.id.clear:
   clear();
   break;
  case R.id.back:
   back();
   break;
  //計(jì)算結(jié)果
  case R.id.result:
   result();
   break;

  default:
   break;

  }
  input.setText(text);
  output.setText(String.valueOf(result));

 }
 };

 private void num(int i) {
 // TODO Auto-generated method stub
 text = text + String.valueOf(i);
 }

 private void dot() {
 // TODO Auto-generated method stub

 text = text + ".";
 }

 private void clear() {
 // TODO Auto-generated method stub

 text = "";
 result = null;
 input.setText("");
 output.setText("");

 }

 private void back() {
 // TODO Auto-generated method stub

 String str = text.substring(0, text.length()-1);
 text = str;

 }

 private void add() {
 // TODO Auto-generated method stub

 text += "+";

 }

 private void sub() {
 // TODO Auto-generated method stub

 text += "-";

 }

 private void multiply() {
 // TODO Auto-generated method stub

 text += "*";

 }

 private void divide() {
 // TODO Auto-generated method stub

 text += "/";
 }

 //計(jì)算輸出結(jié)果
 private void result() {
 // TODO Auto-generated method stub
 result = testOperation(text);
 }


 public Double testOperation(String s){
 //分割字符然后放進(jìn)數(shù)組
 String s1 =s.replace("+","-");
 String[] str = s1.split("-");
 double total1=0;
 //先遍歷數(shù)組,把里面的乘除結(jié)果算出來(lái)
 for(String str1:str){
  if(str1.contains("*")||str1.contains("/")){
  double total = 0;
  for(int i =0;i<str1.length();){
   int count =1;
   a:for(int j =i+1;j<str1.length();j++){
   char c =str1.charAt(j);
   if(c=='*'||c=='/'){
    break a;
   }else{
    count++;
   }
   }

   //將數(shù)字截取出來(lái)
   String s2 =str1.substring(i,i+count);
   double d = Double.parseDouble(s2);
   if(i==0){
   total = d;
   }else{
   char c1 = str1.charAt(i-1);
   if(c1=='*'){
    total*=d;
   }else if(c1=='/'){
    //如果除數(shù)為0,直接返回null;
    if(d == 0)
    return null;
    total/=d;
   }
   }
   i+=count+1;
  }
  s= s.replace(str1, total+"");
  }
 }
 //進(jìn)行加減運(yùn)算
 for(int i =0;i<s.length();i++){
  int count =1;
  a:for(int j=i+1;j<s.length();j++){
  char c = s.charAt(j);
  if(c=='+'||c=='-'){
   break a;
  }else{
   count++;
  }
  }
  String s3= s.substring(i,i+count);
  double d2 = Double.parseDouble(s3);
  if(i==0){
  total1 = d2;
  }else{
  char c = s.charAt(i-1);
  if(c=='+'){
   total1+=d2;
  }else if(c=='-'){
   total1-=d2;
  }
  }
  i+=count;
 }
 return total1;
 }

}

更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專(zhuān)題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)

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

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

相關(guān)文章

  • Android 8.1 Launcher3實(shí)現(xiàn)動(dòng)態(tài)指針時(shí)鐘功能

    Android 8.1 Launcher3實(shí)現(xiàn)動(dòng)態(tài)指針時(shí)鐘功能

    這篇文章主要介紹了Android 8.1 Launcher3實(shí)現(xiàn)動(dòng)態(tài)指針時(shí)鐘功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • 如何正確使用Android線程詳解

    如何正確使用Android線程詳解

    線程是程序員進(jìn)階的一道重要門(mén)檻。除了了解各類(lèi)開(kāi)線程的API之外,更需要理解線程本身到底是個(gè)什么樣的存在,并行是否真的高效?系統(tǒng)是怎么樣去調(diào)度線程的?開(kāi)線程的方式那么多,什么樣的姿勢(shì)才正確?下面通過(guò)本文來(lái)好好再學(xué)習(xí)下。
    2016-08-08
  • Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼

    Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android音頻錄制MediaRecorder之簡(jiǎn)易的錄音軟件實(shí)現(xiàn)代碼,有需要的朋友可以參考一下
    2014-01-01
  • android實(shí)現(xiàn)QQ微信側(cè)滑刪除效果

    android實(shí)現(xiàn)QQ微信側(cè)滑刪除效果

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)QQ微信側(cè)滑刪除效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的方法匯總

    Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的方法匯總

    這篇文章主要介紹了Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的方法匯總,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-06-06
  • Android實(shí)現(xiàn)NFC讀取校園卡

    Android實(shí)現(xiàn)NFC讀取校園卡

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)NFC讀取校園卡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android自定義webView頭部進(jìn)度加載效果

    Android自定義webView頭部進(jìn)度加載效果

    這篇文章主要介紹了Android自定義webView頭部進(jìn)度加載效果,小編畫(huà)一條進(jìn)度線,然后加載webview上面,具體實(shí)現(xiàn)代碼大家參考下本文
    2017-11-11
  • Android微信分享大圖遇到的問(wèn)題的解決方法

    Android微信分享大圖遇到的問(wèn)題的解決方法

    這篇文章主要介紹了Android微信分享大圖遇到的問(wèn)題的解決方法,記錄了微信圖片分享出錯(cuò)的坑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Android中post和get的提交方式【三種】

    Android中post和get的提交方式【三種】

    本文主要對(duì)Android中三種POST和GET的提交方式進(jìn)行詳細(xì)介紹。通過(guò)任何一種方式可以實(shí)現(xiàn)的功能是,從安卓手機(jī)端提交數(shù)據(jù)到服務(wù)器端,服務(wù)器端進(jìn)行判斷,并返回相應(yīng)的結(jié)果。三種方式各有利弊,實(shí)現(xiàn)效果相同,在實(shí)際的使用過(guò)程中可以根據(jù)本身的需要進(jìn)行選擇。
    2016-12-12
  • Android使用AsyncQueryHandler實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能

    Android使用AsyncQueryHandler實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能

    這篇文章主要為大家詳細(xì)介紹了Android使用AsyncQueryHandler實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評(píng)論