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

Android開(kāi)發(fā)學(xué)習(xí)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器

 更新時(shí)間:2020年04月13日 16:38:55   作者:阿檸xn  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

這里是用線(xiàn)性布局實(shí)現(xiàn)的計(jì)算器,為防止以后再回顧知識(shí)代碼找不到,特將代碼貼在這里:

xml文件的布局代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity">

 <EditText
 android:id="@+id/et_input"
 android:layout_width="match_parent"
 android:layout_height="100dp"
 android:paddingBottom="5dp"
 android:paddingRight="5dp"
 android:textSize="50sp"
 android:gravity="right"
 android:textColor="#00ff00"/>


 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="30dp"
 android:orientation="horizontal"
 android:gravity="center_horizontal">

 <Button
  android:id="@+id/btn_clr"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="C"
  android:textSize="30sp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  android:textColor="#ff0000"/>

 <Button
  android:id="@+id/btn_del"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="Ba"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp" />
 <Button
  android:id="@+id/btn_add"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="+"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_sub"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="-"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />



 </LinearLayout>
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="10dp"
 android:orientation="horizontal"
 android:gravity="center_horizontal"
 >
 <Button
  android:id="@+id/btn_7"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="7"
  android:textSize="30sp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_8"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="8"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_9"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="9"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_mul"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="×"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />

 </LinearLayout>
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="10dp"
 android:orientation="horizontal"
 android:gravity="center_horizontal"
 >
 <Button
  android:id="@+id/btn_4"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="4"
  android:textSize="30sp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_5"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="5"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_6"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="6"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_div"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="÷"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp" />

 </LinearLayout>
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:layout_marginTop="10dp"
 android:gravity="center_horizontal">
 <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  >
  <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  >
  <Button
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:id="@+id/btn_1"
   android:text="1"
   android:textSize="30sp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:id="@+id/btn_2"
   android:text="2"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:id="@+id/btn_3"
   android:text="3"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  </LinearLayout>
  <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_marginTop="10dp">
  <Button
   android:layout_width="170dp"
   android:layout_height="80dp"
   android:id="@+id/btn_0"
   android:text="0"
   android:textSize="30sp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:id="@+id/btn_pt"
   android:text="."
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  </LinearLayout>

 </LinearLayout>

 <Button
  android:id="@+id/btn_eq"
  android:layout_width="80dp"
  android:layout_height="170dp"
  android:layout_marginLeft="10dp"
  android:text="="
  android:textSize="30sp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  android:gravity="center"
  android:background="@color/colorAccent"/>

 </LinearLayout>

</LinearLayout>

Activity.java文件實(shí)現(xiàn)功能的代碼如下:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener{

 //創(chuàng)建Button對(duì)象 也就是activity_main.xml里所設(shè)置的ID
 Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_pt;
 Button btn_mul,btn_div,btn_add,btn_sub;
 Button btn_clr,btn_del,btn_eq;
 EditText et_input;
 boolean clr_flag; //判斷et編輯文本框中是否清空

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main2);
 //實(shí)例化對(duì)象
 setContentView(R.layout.activity_main2);
 btn_0= (Button) findViewById(R.id.btn_0);
 btn_1= (Button) findViewById(R.id.btn_1);
 btn_2= (Button) findViewById(R.id.btn_2);
 btn_3= (Button) findViewById(R.id.btn_3);
 btn_4= (Button) findViewById(R.id.btn_4);
 btn_5= (Button) findViewById(R.id.btn_5);
 btn_6= (Button) findViewById(R.id.btn_6);
 btn_7= (Button) findViewById(R.id.btn_7);
 btn_8= (Button) findViewById(R.id.btn_8);
 btn_9= (Button) findViewById(R.id.btn_9);
 btn_pt= (Button) findViewById(R.id.btn_pt);
 btn_add= (Button) findViewById(R.id.btn_add);
 btn_sub= (Button) findViewById(R.id.btn_sub);
 btn_mul= (Button) findViewById(R.id.btn_mul);
 btn_div= (Button) findViewById(R.id.btn_div);
 btn_clr= (Button) findViewById(R.id.btn_clr);
 btn_del= (Button) findViewById(R.id.btn_del);
 btn_eq= (Button) findViewById(R.id.btn_eq);
 et_input= (EditText) findViewById(R.id.et_input);

 //給按鈕設(shè)置的點(diǎn)擊事件
 btn_0.setOnClickListener(this);
 btn_1.setOnClickListener(this);
 btn_2.setOnClickListener(this);
 btn_3.setOnClickListener(this);
 btn_4.setOnClickListener(this);
 btn_5.setOnClickListener(this);
 btn_6.setOnClickListener(this);
 btn_7.setOnClickListener(this);
 btn_8.setOnClickListener(this);
 btn_9.setOnClickListener(this);
 btn_pt.setOnClickListener(this);
 btn_add.setOnClickListener(this);
 btn_sub.setOnClickListener(this);
 btn_mul.setOnClickListener(this);
 btn_div.setOnClickListener(this);
 btn_clr.setOnClickListener(this);
 btn_del.setOnClickListener(this);
 btn_eq.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
 String str=et_input.getText().toString();
 switch (v.getId()){
  case R.id.btn_0:
  case R.id.btn_1:
  case R.id.btn_2:
  case R.id.btn_3:
  case R.id.btn_4:
  case R.id.btn_5:
  case R.id.btn_6:
  case R.id.btn_7:
  case R.id.btn_8:
  case R.id.btn_9:
  case R.id.btn_pt:
  if(clr_flag){
   clr_flag=false;
   str="";
   et_input.setText("");
  }
  et_input.setText(str+((Button)v).getText());
  break;
  case R.id.btn_add:
  case R.id.btn_sub:
  case R.id.btn_mul:
  case R.id.btn_div:
  if(clr_flag){
   clr_flag=false;
   str="";
   et_input.setText("");
  }
  if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷")) {
   str=str.substring(0,str.indexOf(" "));
  }
  et_input.setText(str+" "+((Button)v).getText()+" ");
  break;
  case R.id.btn_clr:
  if(clr_flag)
   clr_flag=false;
  str="";
  et_input.setText("");
  break;
  case R.id.btn_del: //判斷是否為空,然后在進(jìn)行刪除
  if(clr_flag){
   clr_flag=false;
   str="";
   et_input.setText("");
  }
  else if(str!=null&&!str.equals("")){
   et_input.setText(str.substring(0,str.length()-1));
  }
  break;
  case R.id.btn_eq: //單獨(dú)運(yùn)算最后結(jié)果
  getResult();//調(diào)用下面的方法
  break;
 }
 }

 private void getResult() {
 String exp=et_input.getText().toString();
 if(exp==null||exp.equals("")) return ;
 //因?yàn)闆](méi)有運(yùn)算符所以不用運(yùn)算
 if(!exp.contains(" ")){
  return ;
 }
 if(clr_flag){
  clr_flag=false;
  return;
 }
 clr_flag=true;
 //截取運(yùn)算符前面的字符串
 String s1=exp.substring(0,exp.indexOf(" "));
 //截取的運(yùn)算符
 String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
 //截取運(yùn)算符后面的字符串
 String s2=exp.substring(exp.indexOf(" ")+3);
 double cnt=0;
 if(!s1.equals("")&&!s2.equals("")){
  double d1=Double.parseDouble(s1);
  double d2=Double.parseDouble(s2);
  if(op.equals("+")){
  cnt=d1+d2;
  }
  if(op.equals("-")){
  cnt=d1-d2;
  }
  if(op.equals("×")){
  cnt=d1*d2;
  }
  if(op.equals("÷")){
  if(d2==0) cnt=0;
  else cnt=d1/d2;
  }
  if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
  int res = (int) cnt;
  et_input.setText(res+"");
  }else {
  et_input.setText(cnt+"");}
 }
 //如果s1是空 s2不是空 就執(zhí)行下一步
 else if(!s1.equals("")&&s2.equals("")){
  double d1=Double.parseDouble(s1);
  if(op.equals("+")){
  cnt=d1;
  }
  if(op.equals("-")){
  cnt=d1;
  }
  if(op.equals("×")){
  cnt=0;
  }
  if(op.equals("÷")){
  cnt=0;
  }
  if(!s1.contains(".")) {
  int res = (int) cnt;
  et_input.setText(res+"");
  }else {
  et_input.setText(cnt+"");}
 }
 //如果s1是空 s2不是空 就執(zhí)行下一步
 else if(s1.equals("")&&!s2.equals("")){
  double d2=Double.parseDouble(s2);
  if(op.equals("+")){
  cnt=d2;
  }
  if(op.equals("-")){
  cnt=0-d2;
  }
  if(op.equals("×")){
  cnt=0;
  }
  if(op.equals("÷")){
  cnt=0;
  }
  if(!s2.contains(".")) {
  int res = (int) cnt;
  et_input.setText(res+"");
  }else {
  et_input.setText(cnt+"");}
 }
 else {
  et_input.setText("");
 }
 }
}

結(jié)果圖如下所示:

更多計(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開(kāi)發(fā)教程之電源管理詳解

    Android開(kāi)發(fā)教程之電源管理詳解

    這篇文章主要介紹了Android開(kāi)發(fā)教程之電源管理,較為詳細(xì)的分析了Android電源管理的相關(guān)概念、注意事項(xiàng)與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-06-06
  • ViewPager實(shí)現(xiàn)帶引導(dǎo)小圓點(diǎn)與自動(dòng)跳轉(zhuǎn)的引導(dǎo)界面

    ViewPager實(shí)現(xiàn)帶引導(dǎo)小圓點(diǎn)與自動(dòng)跳轉(zhuǎn)的引導(dǎo)界面

    這篇文章主要為大家詳細(xì)介紹了ViewPager實(shí)現(xiàn)帶引導(dǎo)小圓點(diǎn)與自動(dòng)跳轉(zhuǎn)的引導(dǎo)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 一款適用于Android平臺(tái)的俄羅斯方塊

    一款適用于Android平臺(tái)的俄羅斯方塊

    這篇文章主要為大家詳細(xì)介紹了一款適用于Android平臺(tái)的俄羅斯方塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法

    Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法

    這篇文章主要介紹了Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法,涉及Android針對(duì)系統(tǒng)硬件相關(guān)操作技巧,需要的朋友可以參考下
    2016-07-07
  • Monkeyrunner 常用按鍵總結(jié)

    Monkeyrunner 常用按鍵總結(jié)

    這篇文章主要介紹了Monkeyrunner 常用按鍵總結(jié)的相關(guān)資料,這里對(duì)Monkeyrunner 按鍵的功能進(jìn)行詳細(xì)說(shuō)明,需要的朋友可以參考下
    2016-11-11
  • Android 劉海屏適配總結(jié)(推薦)

    Android 劉海屏適配總結(jié)(推薦)

    這篇文章主要介紹了Android 劉海屏適配總結(jié)(推薦),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Android中增加新字庫(kù)的方法

    Android中增加新字庫(kù)的方法

    這篇文章主要介紹了Android中增加新字庫(kù)的方法,本文分別講解了不需要保留Google默認(rèn)的中文字庫(kù)、有需要保留Google默認(rèn)的中文字庫(kù)并在此基礎(chǔ)上增加自己需要的新字庫(kù)兩種情況下的處理方法,需要的朋友可以參考下
    2015-04-04
  • 詳解VirtualApk啟動(dòng)插件Activity

    詳解VirtualApk啟動(dòng)插件Activity

    這篇文章主要介紹了詳解VirtualApk啟動(dòng)插件Activity,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Android Activity啟動(dòng)模式之singleTask實(shí)例詳解

    Android Activity啟動(dòng)模式之singleTask實(shí)例詳解

    這篇文章主要介紹了Android Activity啟動(dòng)模式之singleTask,結(jié)合實(shí)例形式較為詳細(xì)的分析了singleTask模式的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-01-01
  • Android Handler工作原理解析

    Android Handler工作原理解析

    這篇文章主要為大家詳細(xì)介紹了Android Handler的原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02

最新評(píng)論

呼伦贝尔市| 海南省| 合川市| 英德市| 奉化市| 玉林市| 五原县| 连云港市| 禹城市| 合江县| 合山市| 蒲城县| 那曲县| 岑溪市| 都匀市| 新田县| 灯塔市| 靖宇县| 九台市| 新丰县| 东方市| 南昌县| 康马县| 通辽市| 门头沟区| 茂名市| 洛扎县| 潞城市| 柳江县| 谢通门县| 眉山市| 高青县| 连州市| 吉首市| 镇远县| 无为县| 铜鼓县| 民县| 宣恩县| 济南市| 西青区|