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

Android Studio實(shí)現(xiàn)簡易計(jì)算器

 更新時(shí)間:2018年07月11日 14:10:49   作者:RikkaTheWorld  
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)簡易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

如果是制作簡易計(jì)算器的話是基本沒有難點(diǎn)的,供大家參考,具體內(nèi)容如下

步驟是先寫好界面布局,將按鈕的布局、字號顏色啥的做好,再就是設(shè)置監(jiān)聽器。

使用了NoTitleBar的主題

代碼如下:

activity_main里關(guān)于界面的代碼:

<?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="fill_parent"
  android:layout_height="90dp"
  android:background="@drawable/white_bg"
  android:editable="false"
  android:gravity="right|bottom"
  android:paddingBottom="20dp"
  android:paddingRight="20dp"
  android:textSize="50sp" />

 <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/bt_clr"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="C"
  android:gravity="right|bottom"
  android:textSize="30sp"
  android:background="@drawable/white_selector"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
  <Button
   android:id="@+id/bt_del"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="DEL"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
  />
  <Button
   android:id="@+id/bt_div"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="÷"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_mul"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="×"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   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/bt_7"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="7"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_8"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="8"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_9"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="9"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_sub"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="-"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   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/bt_4"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="4"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_5"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="5"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_6"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="6"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_add"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="+"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   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/bt_1"
      android:text="1"
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:background="@drawable/white_selector"
      android:paddingRight="15sp"
      android:paddingBottom="15sp"
      />
     <Button
      android:layout_width="80dp"
      android:layout_height="80dp"
      android:id="@+id/bt_2"
      android:text="2"
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:layout_marginLeft="10dp"
      android:background="@drawable/white_selector"
      android:paddingRight="15sp"
      android:paddingBottom="15sp"
      />
     <Button
      android:layout_width="80dp"
      android:layout_height="80dp"
      android:id="@+id/bt_3"
      android:text="3"
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:layout_marginLeft="10dp"
      android:background="@drawable/white_selector"
      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/bt_0"
      android:text="0"
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:background="@drawable/white_selector"
      android:paddingRight="15sp"
      android:paddingBottom="15sp"
      />
     <Button
      android:layout_width="80dp"
      android:layout_height="80dp"
      android:id="@+id/bt_pt"
      android:text="."
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:layout_marginLeft="10dp"
      android:background="@drawable/white_selector"
      android:paddingRight="15sp"
      android:paddingBottom="15sp"
      />
    </LinearLayout>

   </LinearLayout>

   <Button
    android:id="@+id/bt_eq"
    android:layout_width="80dp"
    android:layout_height="170dp"
    android:layout_marginLeft="10dp"
    android:background="@drawable/orange_selector"
    android:gravity="right|bottom"
    android:text="="
    android:textSize="30sp"
    android:paddingRight="15sp"
    android:paddingBottom="15sp"
    />

  </LinearLayout>
</LinearLayout>

Mainactivity的代碼:

package com.example.administrator.calculatordemo;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener{
 Button bt_0,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_pt;
 Button bt_mul,bt_div,bt_add,bt_sub;
 Button bt_clr,bt_del,bt_eq;
 EditText et_input;
 boolean clr_flag; //判斷et中是否清空
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //實(shí)例化對象
  setContentView(R.layout.activity_main);
  bt_0= (Button) findViewById(R.id.bt_0);
  bt_1= (Button) findViewById(R.id.bt_1);
  bt_2= (Button) findViewById(R.id.bt_2);
  bt_3= (Button) findViewById(R.id.bt_3);
  bt_4= (Button) findViewById(R.id.bt_4);
  bt_5= (Button) findViewById(R.id.bt_5);
  bt_6= (Button) findViewById(R.id.bt_6);
  bt_7= (Button) findViewById(R.id.bt_7);
  bt_8= (Button) findViewById(R.id.bt_8);
  bt_9= (Button) findViewById(R.id.bt_9);
  bt_pt= (Button) findViewById(R.id.bt_pt);
  bt_add= (Button) findViewById(R.id.bt_add);
  bt_sub= (Button) findViewById(R.id.bt_sub);
  bt_mul= (Button) findViewById(R.id.bt_mul);
  bt_div= (Button) findViewById(R.id.bt_div);
  bt_clr= (Button) findViewById(R.id.bt_clr);
  bt_del= (Button) findViewById(R.id.bt_del);
  bt_eq= (Button) findViewById(R.id.bt_eq);
  et_input= (EditText) findViewById(R.id.et_input);

  //設(shè)置按鈕的點(diǎn)擊事件
  bt_0.setOnClickListener(this);
  bt_1.setOnClickListener(this);
  bt_2.setOnClickListener(this);
  bt_3.setOnClickListener(this);
  bt_4.setOnClickListener(this);
  bt_5.setOnClickListener(this);
  bt_6.setOnClickListener(this);
  bt_7.setOnClickListener(this);
  bt_8.setOnClickListener(this);
  bt_9.setOnClickListener(this);
  bt_pt.setOnClickListener(this);
  bt_add.setOnClickListener(this);
  bt_sub.setOnClickListener(this);
  bt_mul.setOnClickListener(this);
  bt_div.setOnClickListener(this);
  bt_clr.setOnClickListener(this);
  bt_del.setOnClickListener(this);
  bt_eq.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  String str=et_input.getText().toString();
   switch (v.getId()){
    case R.id.bt_0:
    case R.id.bt_1:
    case R.id.bt_2:
    case R.id.bt_3:
    case R.id.bt_4:
    case R.id.bt_5:
    case R.id.bt_6:
    case R.id.bt_7:
    case R.id.bt_8:
    case R.id.bt_9:
    case R.id.bt_pt:
     if(clr_flag){
      clr_flag=false;
      str="";
      et_input.setText("");
     }
     et_input.setText(str+((Button)v).getText());
     break;
    case R.id.bt_add:
    case R.id.bt_sub:
    case R.id.bt_mul:
    case R.id.bt_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.bt_clr:
     if(clr_flag)
      clr_flag=false;
     str="";
     et_input.setText("");
     break;
    case R.id.bt_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.bt_eq: //單獨(dú)運(yùn)算最后結(jié)果
     getResult();
     break;
   }
 }

 private void getResult(){
   String exp=et_input.getText().toString();
  if(exp==null||exp.equals("")) return ;
  //因?yàn)闆]有運(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為空
  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不是空
  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("");
  }
 }
}

關(guān)于計(jì)算器的精彩文章請查看《計(jì)算器專題》 ,更多精彩等你來發(fā)現(xiàn)!

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

相關(guān)文章

  • android實(shí)現(xiàn)簡單儀表盤效果

    android實(shí)現(xiàn)簡單儀表盤效果

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡單儀表盤效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android Binder入門學(xué)習(xí)筆記

    Android Binder入門學(xué)習(xí)筆記

    這篇文章主要給大家介紹了關(guān)于Android Binder入門學(xué)習(xí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • Android?拍照功能實(shí)現(xiàn)(手機(jī)關(guān)閉依然拍照)詳解及實(shí)例代碼

    Android?拍照功能實(shí)現(xiàn)(手機(jī)關(guān)閉依然拍照)詳解及實(shí)例代碼

    這篇文章主要介紹了?Android?拍照功能實(shí)現(xiàn)(手機(jī)關(guān)閉依然拍照)詳解及實(shí)例代碼的相關(guān)資料,這對Android相機(jī)在不開手機(jī)的情況下還能繼續(xù)拍照,附有實(shí)例Demo,需要的朋友可以參考下
    2016-12-12
  • Android自定義View實(shí)現(xiàn)雪花特效

    Android自定義View實(shí)現(xiàn)雪花特效

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)雪花特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • Android串口通訊SerialPort的使用詳情

    Android串口通訊SerialPort的使用詳情

    這篇文章主要介紹了Android串口通訊SerialPort的使用詳情,文章圍繞主題展開詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-09-09
  • Android手機(jī)內(nèi)存中文件的讀寫方法小結(jié)

    Android手機(jī)內(nèi)存中文件的讀寫方法小結(jié)

    這篇文章主要介紹了Android手機(jī)內(nèi)存中文件的讀寫方法,實(shí)例總結(jié)了Android針對文件讀寫操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Android 使用PDF.js瀏覽pdf的方法示例

    Android 使用PDF.js瀏覽pdf的方法示例

    這篇文章主要介紹了Android 使用PDF.js瀏覽pdf的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • Android實(shí)現(xiàn)九宮格圖案解鎖

    Android實(shí)現(xiàn)九宮格圖案解鎖

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)九宮格圖案解鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android Studio 3.1.3升級至3.6.1后舊項(xiàng)目的兼容操作方法

    Android Studio 3.1.3升級至3.6.1后舊項(xiàng)目的兼容操作方法

    這篇文章主要介紹了Android Studio 3.1.3升級至3.6.1后舊項(xiàng)目的兼容操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • android實(shí)現(xiàn)關(guān)閉或開啟移動網(wǎng)絡(luò)數(shù)據(jù)

    android實(shí)現(xiàn)關(guān)閉或開啟移動網(wǎng)絡(luò)數(shù)據(jù)

    本篇文章是對android實(shí)現(xiàn)關(guān)閉或開啟移動網(wǎng)絡(luò)數(shù)據(jù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評論

视频| 镇赉县| 霍邱县| 黄冈市| 聂拉木县| 万源市| 天等县| 濉溪县| 外汇| 称多县| 洛川县| 确山县| 志丹县| 开原市| 文登市| 武胜县| 大庆市| 拉萨市| 米脂县| 松桃| 白山市| 桐庐县| 顺平县| 浦北县| 思南县| 浦县| 龙南县| 雷州市| 东乌珠穆沁旗| 南投县| 阿尔山市| 洪江市| 锡林浩特市| 寻乌县| 松滋市| 文安县| 浏阳市| 万宁市| 肇庆市| 大余县| 北海市|