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

Android計(jì)算器編寫代碼

 更新時(shí)間:2016年07月05日 11:19:26   作者:yongh701  
這篇文章主要為大家分享了Android計(jì)算器編寫代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

其實(shí)這個(gè)安卓計(jì)算機(jī),所有的后臺(tái)思想與《C#計(jì)算器編寫代碼》是一模一樣的。Win窗體程序移植到安卓,從C#到Java其實(shí)很簡(jiǎn)單的,因?yàn)閮烧叩幕菊Z(yǔ)法都很相像,唯一的難點(diǎn)是安卓的xml布局部分,不像C#窗體能夠直接拖。
 還是如下圖一個(gè)能夠完成基本四則運(yùn)算的計(jì)算器: 

先在res\values\strings.xml設(shè)置按鈕相應(yīng)的字體,以免布局文件警告滿天飛:

 <?xml version="1.0" encoding="utf-8"?>
<resources>

 <string name="app_name">計(jì)算器</string>
 <string name="bt_1">1</string>
 <string name="bt_2">2</string>
 <string name="bt_3">3</string>
 <string name="bt_4">4</string>
 <string name="bt_5">5</string>
 <string name="bt_6">6</string>
 <string name="bt_7">7</string>
 <string name="bt_8">8</string>
 <string name="bt_9">9</string>
 <string name="bt_0">0</string>
 <string name="bt_point">.</string>
 <string name="bt_ce">CE</string>
 <string name="bt_plus">+</string>
 <string name="bt_minus">-</string>
 <string name="bt_multi">×</string>
 <string name="bt_div">÷</string>
 <string name="bt_result">=</string>

</resources>

 之后,布局部分采用了《【Android】關(guān)于百分比布局多個(gè)LinearLayout嵌套時(shí)出現(xiàn)的問題與解決方案》(點(diǎn)擊打開鏈接)的思想,具體如下圖,一個(gè)TextView、一個(gè)EditText,皆直接用match_parent占據(jù)整行的寬度,之后利用LinearLayout與TableLayout作橫向比例的劃分。 

因此,res\layout\activity_main.xml具體代碼如下,之后的操作要操作的組件加上Id,這里加上《【Android】?jī)?nèi)存卡圖片讀取器,圖庫(kù)app》(點(diǎn)擊打開鏈接)的ScrollView是防止某些手機(jī)屏幕過少,加上垂直滾動(dòng)條:

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

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

  <TextView
   android:id="@+id/textView1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />

  <EditText
   android:id="@+id/editText1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:enabled="false"
   android:inputType="none"
   android:textSize="18sp" />

  <LinearLayout
   android:baselineAligned="false"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <TableLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" >

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >

     <Button
      android:id="@+id/bt_7" 
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_7" />

     <Button
      android:id="@+id/bt_8" 
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_8" />

     <Button
      android:id="@+id/bt_9" 
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_9" />
    </LinearLayout>

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >

     <Button
      android:id="@+id/bt_4" 
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_4" />

     <Button
      android:id="@+id/bt_5" 
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_5" />

     <Button
      android:id="@+id/bt_6"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_6" />
    </LinearLayout>

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >

     <Button
      android:id="@+id/bt_1"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_1" />

     <Button
      android:id="@+id/bt_2"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_2" />

     <Button
      android:id="@+id/bt_3"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_3" />
    </LinearLayout>

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >

     <Button
      android:id="@+id/bt_0"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_0" />

     <Button
      android:id="@+id/bt_point"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_point" />
    </LinearLayout>
   </TableLayout>

   <TableLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <Button
     android:id="@+id/bt_ce"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/bt_ce" />

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >

     <Button
      android:id="@+id/bt_plus"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_plus" />

     <Button
      android:id="@+id/bt_minus"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_minus" />
    </LinearLayout>

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >

     <Button
      android:id="@+id/bt_multi"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_multi" />

     <Button
      android:id="@+id/bt_div"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="@string/bt_div" />
    </LinearLayout>

    <Button
     android:id="@+id/bt_result"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/bt_result" />
   </TableLayout>
  </LinearLayout>
 </LinearLayout>

</ScrollView>

之后是MainActivity.java沒什么好說的,基本與直接Win窗體的《C#計(jì)算器編寫代碼》,將C#改成java是一個(gè)很簡(jiǎn)單的事情。唯一注意的是,這里的按鈕比較多,因此不建議像《【Android】利用Java代碼布局,按鈕添加點(diǎn)擊事件》(點(diǎn)擊打開鏈接)一樣,使用內(nèi)部匿名類實(shí)現(xiàn)按鈕的點(diǎn)擊事件,應(yīng)該讓MainActivity實(shí)現(xiàn)OnClickListener接口,之后在繼承下來的onClick方法,根據(jù)傳遞過來的View v中的id,利用switch-case結(jié)構(gòu)來搞,這樣清晰明了。

 package com.calculator;

import java.util.*;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {

 private List<Double> value_list = new ArrayList<Double>();// 存用戶輸入的數(shù)字
 private List<Integer> operator_list = new ArrayList<Integer>();// 存用戶輸入的運(yùn)算符,定義+為0,-為1,×為2,÷為3
 // 狀態(tài)記錄
 private boolean add_flag = false;// +按下
 private boolean minus_flag = false;// -按下
 private boolean multi_flag = false;// ×按下
 private boolean div_flag = false;// ÷按下
 private boolean result_flag = false;// =按下
 private boolean can_operate_flag = false;// 按下=是否響應(yīng)

 private TextView textView1;
 private EditText editText1;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 findViewById(R.id.bt_0).setOnClickListener(this);
 findViewById(R.id.bt_1).setOnClickListener(this);
 findViewById(R.id.bt_2).setOnClickListener(this);
 findViewById(R.id.bt_3).setOnClickListener(this);
 findViewById(R.id.bt_4).setOnClickListener(this);
 findViewById(R.id.bt_5).setOnClickListener(this);
 findViewById(R.id.bt_6).setOnClickListener(this);
 findViewById(R.id.bt_7).setOnClickListener(this);
 findViewById(R.id.bt_8).setOnClickListener(this);
 findViewById(R.id.bt_9).setOnClickListener(this);
 findViewById(R.id.bt_point).setOnClickListener(this);
 findViewById(R.id.bt_ce).setOnClickListener(this);
 findViewById(R.id.bt_plus).setOnClickListener(this);
 findViewById(R.id.bt_minus).setOnClickListener(this);
 findViewById(R.id.bt_multi).setOnClickListener(this);
 findViewById(R.id.bt_div).setOnClickListener(this);
 findViewById(R.id.bt_result).setOnClickListener(this);
 textView1 = (TextView) findViewById(R.id.textView1);
 editText1 = (EditText) findViewById(R.id.editText1);

 }

 @Override
 public void onClick(View v) {
 switch (v.getId()) {
 case R.id.bt_0:
 num_down("0");
 break;
 case R.id.bt_1:
 num_down("1");
 break;
 case R.id.bt_2:
 num_down("2");
 break;
 case R.id.bt_3:
 num_down("3");
 break;
 case R.id.bt_4:
 num_down("4");
 break;
 case R.id.bt_5:
 num_down("5");
 break;
 case R.id.bt_6:
 num_down("6");
 break;
 case R.id.bt_7:
 num_down("7");
 break;
 case R.id.bt_8:
 num_down("8");
 break;
 case R.id.bt_9:
 num_down("9");
 break;
 case R.id.bt_point:
 num_down(".");
 break;
 case R.id.bt_plus:
 if (!add_flag)// 防止用戶多次輸入一個(gè)符號(hào)鍵,符號(hào)鍵只允許輸入一次
 {
 result_flag = false;
 value_list.add(Double.parseDouble(editText1.getText()
  .toString()));// 將當(dāng)前已輸入的數(shù)字放入value_list
 operator_list.add(0);
 textView1.setText(textView1.getText() + "+");
 add_flag = true;
 can_operate_flag = false;// 剛剛輸入完符號(hào),不能構(gòu)成一條正常的表達(dá)式,如111+,設(shè)置為不可運(yùn)行狀態(tài)
 }
 break;
 case R.id.bt_minus:
 if (!minus_flag) {
 result_flag = false;
 value_list.add(Double.parseDouble(editText1.getText()
  .toString()));
 operator_list.add(1);
 textView1.setText(textView1.getText() + "-");
 minus_flag = true;
 can_operate_flag = false;
 }
 break;
 case R.id.bt_multi:
 if (!multi_flag) {
 result_flag = false;
 value_list.add(Double.parseDouble(editText1.getText()
  .toString()));
 operator_list.add(2);
 textView1.setText("(" + textView1.getText() + ")×");// 給前面的已經(jīng)輸入的東西加個(gè)括號(hào)。(運(yùn)算符棧問題是一個(gè)很復(fù)雜的數(shù)據(jù)結(jié)構(gòu)問題,這里不做,:P)
 multi_flag = true;
 can_operate_flag = false;
 }
 break;
 case R.id.bt_div:
 if (!div_flag) {
 result_flag = false;
 value_list.add(Double.parseDouble(editText1.getText()
  .toString()));
 operator_list.add(3);
 textView1.setText("(" + textView1.getText() + ")÷");
 div_flag = true;
 can_operate_flag = false;
 }
 break;
 case R.id.bt_result:
 if (value_list.size() > 0 && operator_list.size() > 0
  && can_operate_flag) {// 需要防止用戶沒輸入數(shù)字,或者只輸入了一個(gè)數(shù),就按=。
 value_list.add(Double.parseDouble(editText1.getText()
  .toString()));
 double total = value_list.get(0);
 for (int i = 0; i < operator_list.size(); i++) {
  int _operator = operator_list.get(i);// operator是C#的運(yùn)算符重載的關(guān)鍵字,前面加個(gè)_來區(qū)別
  switch (_operator) {
  case 0:
  total += value_list.get(i + 1);
  break;
  case 1:
  total -= value_list.get(i + 1);
  break;
  case 2:
  total *= value_list.get(i + 1);
  break;
  case 3:
  total /= value_list.get(i + 1);
  break;
  }
 }
 editText1.setText(total + "");
 textView1.setText(total + "");
 operator_list.clear();// 算完,就清空累積數(shù)字與運(yùn)算數(shù)組
 value_list.clear();
 result_flag = true;// 表示=按下
 }
 break;
 case R.id.bt_ce:
 operator_list.clear();
 value_list.clear();
 add_flag = false;
 minus_flag = false;
 multi_flag = false;
 div_flag = false;
 result_flag = false;
 can_operate_flag = false;
 editText1.setText("");
 textView1.setText("");
 break;
 }
 }

 // 數(shù)字鍵按下,含0與.,類似000001223這類情況這里允許,因?yàn)閖ava可以講000001223自己轉(zhuǎn)化為1223
 private void num_down(String num) {
 if (add_flag || minus_flag || multi_flag || div_flag || result_flag) {
 if (result_flag)// 按下等號(hào),剛剛算完一個(gè)運(yùn)算的狀態(tài)
 {
 textView1.setText("");
 }
 editText1.setText("");// 如果用戶剛剛輸入完一個(gè)運(yùn)算符
 add_flag = false;
 minus_flag = false;
 multi_flag = false;
 div_flag = false;
 result_flag = false;
 }
 if ((num.equals(".") && editText1.getText().toString().indexOf(".") < 0)
 || !num.equals(".")) {
 // 如果用戶輸入的是小數(shù)點(diǎn).,則要判斷當(dāng)前已輸入的數(shù)字中是否含有小數(shù)點(diǎn).才允許輸入
 editText1.setText(editText1.getText() + num);
 textView1.setText(textView1.getText() + num);
 can_operate_flag = true;
 }
 }

}

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

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

相關(guān)文章

最新評(píng)論

随州市| 阜阳市| 龙山县| 隆德县| 南召县| 海晏县| 呼和浩特市| 大厂| 德州市| 封开县| 乡城县| 湟中县| 五寨县| 永泰县| 子长县| 阿勒泰市| 巴东县| 宜宾县| 财经| 恭城| 广河县| 洛扎县| 甘洛县| 新郑市| 广西| 昌宁县| 湄潭县| 扎囊县| 呼图壁县| 平定县| 江城| 岑溪市| 朝阳区| 娱乐| 溆浦县| 鄂托克旗| 莲花县| 格尔木市| 桓仁| 邵阳市| 中宁县|