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

Android開發(fā)學習實現簡單計算器

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

這里是用線性布局實現的計算器,為防止以后再回顧知識代碼找不到,特將代碼貼在這里:

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文件實現功能的代碼如下:

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對象 也就是activity_main.xml里所設置的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);
 //實例化對象
 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);

 //給按鈕設置的點擊事件
 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: //判斷是否為空,然后在進行刪除
  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: //單獨運算最后結果
  getResult();//調用下面的方法
  break;
 }
 }

 private void getResult() {
 String exp=et_input.getText().toString();
 if(exp==null||exp.equals("")) return ;
 //因為沒有運算符所以不用運算
 if(!exp.contains(" ")){
  return ;
 }
 if(clr_flag){
  clr_flag=false;
  return;
 }
 clr_flag=true;
 //截取運算符前面的字符串
 String s1=exp.substring(0,exp.indexOf(" "));
 //截取的運算符
 String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
 //截取運算符后面的字符串
 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("");
 }
 }
}

結果圖如下所示:

更多計算器功能實現,請點擊專題: 計算器功能匯總 進行學習

關于Android計算器功能的實現,查看專題:Android計算器 進行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android使用Scroller實現彈性滑動效果

    Android使用Scroller實現彈性滑動效果

    這篇文章主要介紹了Android使用Scroller實現彈性滑動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android6.0動態(tài)申請權限所遇到的問題小結

    Android6.0動態(tài)申請權限所遇到的問題小結

    這篇文章給大家介紹了Android6.0動態(tài)申請權限所遇到的問題,在沒給大家介紹這下問題之前,先給大家說下基本定義和基本使用方式,本文給大家介紹的非常詳細,具有參考借鑒價值,對android 6.0 動態(tài)權限遇到問題感興趣的朋友一起看看吧
    2016-11-11
  • Android控件Spinner實現下拉列表及監(jiān)聽功能

    Android控件Spinner實現下拉列表及監(jiān)聽功能

    這篇文章主要介紹了Android控件Spinner實現下拉列表及監(jiān)聽功能,這是在Web開發(fā)中一個必不可少的交互性組件,而在Android中的對應實現就是Spinner。需要的朋友可以參考下
    2018-07-07
  • Android中TabLayout結合ViewPager實現頁面切換效果

    Android中TabLayout結合ViewPager實現頁面切換效果

    這篇文章主要介紹了Android中TabLayout結合ViewPager實現頁面切換效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android自定義商品購買數量加減控件

    Android自定義商品購買數量加減控件

    這篇文章主要為大家詳細介紹了Android自定義商品購買數量加減控件的相關代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • android ListView結合xutils3仿微信實現下拉加載更多

    android ListView結合xutils3仿微信實現下拉加載更多

    本篇文章主要介紹了android ListView結合xutils3仿微信實現下拉加載更多,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android 歡迎全屏圖片詳解及實例代碼

    Android 歡迎全屏圖片詳解及實例代碼

    這篇文章主要介紹了Android 歡迎全屏圖片詳解及實例代碼的相關資料,需要的朋友可以參考下
    2017-02-02
  • Flutter實現webview與原生組件組合滑動的示例代碼

    Flutter實現webview與原生組件組合滑動的示例代碼

    這篇文章主要介紹了Flutter實現webview與原生組件組合滑動的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • Android 解析XML 文件的四種方法總結

    Android 解析XML 文件的四種方法總結

    本文將詳細介紹用解析XML的四種方法,XML現在已經成為一種通用的數據交換格式,平臺的無關性使得很多場合都需要用到XML,這里對幾種解析XML 的方法做詳解
    2016-07-07
  • 基于Python的Android圖形解鎖程序詳解

    基于Python的Android圖形解鎖程序詳解

    這篇文章主要介紹了基于Python的Android圖形解鎖程序詳解,具有一定借鑒價值,需要的朋友可以參考下。
    2017-11-11

最新評論