Android table布局開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
本文實(shí)例為大家分享了Android table布局開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
結(jié)果如圖:

XML文件如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.wxhcalculator.MainActivity" tools:ignore="MergeRootFrame" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1" android:textSize="42sp" > <TableRow> <EditText android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_span="4" android:background="@android:drawable/editbox_background" android:cursorVisible="false" android:editable="false" android:gravity="right|center_vertical" android:lines="1" android:textSize="60sp" /> </TableRow> <TableRow> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:textSize="42sp" > <Button android:id="@+id/num7" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" android:textSize="42sp" /> <Button android:id="@+id/num8" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" android:textSize="42sp" /> <Button android:id="@+id/num9" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" android:textSize="42sp" /> <Button android:id="@+id/divide" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="/" android:textSize="42sp" /> </LinearLayout> </TableRow> <TableRow> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:textSize="42sp" > <Button android:id="@+id/num4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" android:textSize="42sp" /> <Button android:id="@+id/num5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" android:textSize="42sp" /> <Button android:id="@+id/num6" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" android:textSize="42sp" /> <Button android:id="@+id/multiply" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="*" android:textSize="42sp" /> </LinearLayout> </TableRow> <TableRow> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:textSize="42sp" > <Button android:id="@+id/num1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" android:textSize="42sp" /> <Button android:id="@+id/num2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" android:textSize="42sp" /> <Button android:id="@+id/num3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" android:textSize="42sp" /> <Button android:id="@+id/subtract" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="-" android:textSize="42sp" /> </LinearLayout> </TableRow> <TableRow> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:textSize="42sp" > <Button android:id="@+id/num0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" android:textSize="42sp" /> <Button android:id="@+id/point" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="." android:textSize="42sp" /> <Button android:id="@+id/add" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" android:textSize="42sp" /> <Button android:id="@+id/equal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="=" android:textSize="42sp" /> </LinearLayout> </TableRow> <TableRow> <Button android:id="@+id/clear" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_span="4" android:gravity="center_vertical|center_horizontal" android:text="clear" android:textSize="30sp" /> </TableRow> </TableLayout> </FrameLayout>
mainActivity主函數(shù)如下:
package com.example.wxhcalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private Button[] btnNum = new Button[11];// 數(shù)值按鈕
private Button[] btnCommand = new Button[5];// 符號(hào)按鈕
private EditText editText = null;// 顯示區(qū)域
private Button btnClear = null; // clear按鈕
private String lastCommand; // 用于保存運(yùn)算符
private boolean clearFlag; // 用于判斷是否清空顯示區(qū)域的值,true需要,false不需要
private boolean firstFlag; // 用于判斷是否是首次輸入,true首次,false不是首次
private double result; // 計(jì)算結(jié)果
public MainActivity() {
// 初始化各項(xiàng)值
result = 0; // x的值
firstFlag = true; // 是首次運(yùn)算
clearFlag = false; // 不需要清空
lastCommand = "="; // 運(yùn)算符
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲取運(yùn)算符
btnCommand[0] = (Button) findViewById(R.id.add);
btnCommand[1] = (Button) findViewById(R.id.subtract);
btnCommand[2] = (Button) findViewById(R.id.multiply);
btnCommand[3] = (Button) findViewById(R.id.divide);
btnCommand[4] = (Button) findViewById(R.id.equal);
// 獲取數(shù)字
btnNum[0] = (Button) findViewById(R.id.num0);
btnNum[1] = (Button) findViewById(R.id.num1);
btnNum[2] = (Button) findViewById(R.id.num2);
btnNum[3] = (Button) findViewById(R.id.num3);
btnNum[4] = (Button) findViewById(R.id.num4);
btnNum[5] = (Button) findViewById(R.id.num5);
btnNum[6] = (Button) findViewById(R.id.num6);
btnNum[7] = (Button) findViewById(R.id.num7);
btnNum[8] = (Button) findViewById(R.id.num8);
btnNum[9] = (Button) findViewById(R.id.num9);
btnNum[10] = (Button) findViewById(R.id.point);
// 初始化顯示結(jié)果區(qū)域
editText = (EditText) findViewById(R.id.result);
editText.setText("0.0");
// 實(shí)例化監(jiān)聽(tīng)器對(duì)象
NumberAction na = new NumberAction();
CommandAction ca = new CommandAction();
for (Button bc : btnCommand) {
bc.setOnClickListener(ca);
}
for (Button bc : btnNum) {
bc.setOnClickListener(na);
}
// clear按鈕的動(dòng)作
btnClear = (Button) findViewById(R.id.clear);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editText.setText("0.0");
// 初始化各項(xiàng)值
result = 0; // x的值
firstFlag = true; // 是首次運(yùn)算
clearFlag = false; // 不需要清空
lastCommand = "="; // 運(yùn)算符
}
});
}
// 數(shù)字按鈕監(jiān)聽(tīng)器
private class NumberAction implements OnClickListener {
@Override
public void onClick(View view) {
Button btn = (Button) view;
String input = btn.getText().toString();
if (firstFlag) { // 首次輸入
// 一上就".",就什么也不做
if (input.equals(".")) {
return;
}
// 如果是"0.0"的話,就清空
if (editText.getText().toString().equals("0.0")) {
editText.setText("");
}
firstFlag = false;// 改變是否首次輸入的標(biāo)記值
} else {
String editTextStr = editText.getText().toString();
// 判斷顯示區(qū)域的值里面是否已經(jīng)有".",如果有,輸入的又是".",就什么都不做
if (editTextStr.indexOf(".") != -1 && input.equals(".")) {
return;
}
// 判斷顯示區(qū)域的值里面只有"-",輸入的又是".",就什么都不做
if (editTextStr.equals("-") && input.equals(".")) {
return;
}
// 判斷顯示區(qū)域的值如果是"0",輸入的不是".",就什么也不做
if (editTextStr.equals("0") && !input.equals(".")) {
return;
}
}
// 如果我點(diǎn)擊了運(yùn)算符以后,再輸入數(shù)字的話,就要清空顯示區(qū)域的值
if (clearFlag) {
editText.setText("");
clearFlag = false;// 還原初始值,不需要清空
}
editText.setText(editText.getText().toString() + input);// 設(shè)置顯示區(qū)域的值
}
}
// 符號(hào)按鈕監(jiān)聽(tīng)器
private class CommandAction implements OnClickListener {
@Override
public void onClick(View view) {
Button btn = (Button) view;
String inputCommand = (String) btn.getText();
if (firstFlag) {// 首次輸入"-"的情況
if (inputCommand.equals("-")) {
editText.setText("-");// 顯示區(qū)域的內(nèi)容設(shè)置為"-"
firstFlag = false;// 改變首次輸入的標(biāo)記
}
} else {
if (!clearFlag) {// 如果flag=false不需要清空顯示區(qū)的值,就調(diào)用方法計(jì)算
calculate(Double.parseDouble(editText.getText().toString()));// 保存顯示區(qū)域的值,并計(jì)算
}
// 保存你點(diǎn)擊的運(yùn)算符
lastCommand = inputCommand;
clearFlag = true;// 因?yàn)槲疫@里已經(jīng)輸入過(guò)運(yùn)算符,
}
}
}
// 計(jì)算用的方法
private void calculate(double x) {
if (lastCommand.equals("+")) {
result += x;
} else if (lastCommand.equals("-")) {
result -= x;
} else if (lastCommand.equals("*")) {
result *= x;
} else if (lastCommand.equals("/")) {
result /= x;
} else if (lastCommand.equals("=")) {
result = x;
}
editText.setText("" + result);
}
}
更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)
關(guān)于Android計(jì)算器功能的實(shí)現(xiàn),查看專題:Android計(jì)算器 進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 從零開(kāi)始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
- Android開(kāi)發(fā)實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器功能【附完整demo源碼下載】
- android計(jì)算器簡(jiǎn)單實(shí)現(xiàn)代碼
- Android計(jì)算器編寫(xiě)代碼
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
- Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App
- android studio實(shí)現(xiàn)計(jì)算器
- android計(jì)算器代碼示例分享
- 簡(jiǎn)單實(shí)現(xiàn)Android計(jì)算器功能
相關(guān)文章
android中圖片翻頁(yè)效果簡(jiǎn)單的實(shí)現(xiàn)方法
android中圖片翻頁(yè)效果簡(jiǎn)單的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-05-05
Android自定義View實(shí)現(xiàn)選座功能
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)選座功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Android local.properties 文件讀取實(shí)例詳解
這篇文章主要介紹了Android local.properties 文件讀取實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android Studio+MAT實(shí)戰(zhàn)內(nèi)存泄漏
這篇文章主要介紹了Android Studio+MAT實(shí)戰(zhàn)內(nèi)存泄漏的相關(guān)技術(shù)內(nèi)容,并在需要注意的地方做了提示,需要參考學(xué)習(xí)下吧。2017-12-12
解決Could not find com.android.tools.build:gradle:3.0.0
這篇文章主要介紹了在Android Studio升級(jí)時(shí)碰到Could not find com.android.tools.build:gradle:3.0.0問(wèn)題的解決方法,需要的朋友跟隨小編一起看看吧2021-08-08
Android利用CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能 Android實(shí)現(xiàn)停留5s跳轉(zhuǎn)到登錄頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android利用CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能,Android實(shí)現(xiàn)停留5s跳轉(zhuǎn)到登錄頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
ViewPager+PagerAdapter實(shí)現(xiàn)帶指示器的引導(dǎo)頁(yè)
這篇文章主要為大家詳細(xì)介紹了ViewPager+PagerAdapter實(shí)現(xiàn)帶指示器的引導(dǎo)頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Android RecyclerView仿新聞?lì)^條的頻道管理功能
這篇文章主要介紹了Android RecyclerView仿新聞?lì)^條的頻道管理功能,需要的朋友可以參考下2017-06-06
詳解Android WebView的input上傳照片的兼容問(wèn)題
本篇文章主要介紹了詳解Android WebView的input上傳照片的兼容問(wèn)題,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08

