activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:weightSum="2" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:id="@+id/lnScreen" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/lnNumber" android:background="#f9c992"> <EditText android:paddingLeft="5dp" android:gravity="left|center" android:textColor="#131312" android:textSize="50sp" android:id="@+id/edTinh" android:layout_width="match_parent" android:layout_height="80dp" android:background="#50d227" android:layout_weight="0.74" /> <TextView android:gravity="right|bottom" android:textColor="#50d227" android:textSize="50sp" android:background="#1a1b1b" android:id="@+id/tvKetqua" android:layout_width="match_parent" android:layout_height="80dp" /> </LinearLayout> <LinearLayout android:background="#000" android:layout_alignParentBottom="true" android:layout_centerInParent="true" android:id="@+id/lnNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:weightSum="5"> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="80dp" android:weightSum="4"> <Button android:id="@+id/ac" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="AC" /> <Button android:id="@+id/c" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="C" /> <Button android:id="@+id/phanTram" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="%" /> <Button android:id="@+id/chia" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="/" /> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="80dp" android:weightSum="4"> <Button android:id="@+id/num7" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="7" /> <Button android:id="@+id/num8" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="8" /> <Button android:id="@+id/num9" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="9" /> <Button android:id="@+id/nhan" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="x" /> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="80dp" android:weightSum="4"> <Button android:id="@+id/num4" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="4" /> <Button android:id="@+id/num5" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="5" /> <Button android:id="@+id/num6" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="6" /> <Button android:id="@+id/cong" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="+" /> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="80dp" android:weightSum="4"> <Button android:id="@+id/num1" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="1" /> <Button android:id="@+id/num2" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="2" /> <Button android:id="@+id/num3" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="3" /> <Button android:id="@+id/tru" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="----" /> </LinearLayout> <LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="80dp" android:weightSum="4"> <Button android:id="@+id/num0" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="3.5" android:text="0" /> <Button android:id="@+id/cham" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.2" android:text="." /> <Button android:id="@+id/bang" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.3" android:text="=" /> </LinearLayout> </LinearLayout> </RelativeLayout>
MainActivity.java
Java Android 2017
package com.example.lonely.maytinhbotui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.BaseInputConnection;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText edNumer;
private TextView tvResult;
private Button btnNum0, btnNum1, btnNum2, btnNum3, btnNum4, btnNum5,btnNum6, btnNum7,btnNum8,btnNum9;
private Button btnCong, btnTru, btnNhan, btnChia,btnBang;
private Button btnCham, btnPhantram, btnC, btnAc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getValue();
btnNum0.setOnClickListener(this);
btnNum1.setOnClickListener(this);
btnNum2.setOnClickListener(this);
btnNum3.setOnClickListener(this);
btnNum4.setOnClickListener(this);
btnNum5.setOnClickListener(this);
btnNum6.setOnClickListener(this);
btnNum7.setOnClickListener(this);
btnNum8.setOnClickListener(this);
btnNum9.setOnClickListener(this);
btnAc.setOnClickListener(this);
btnC.setOnClickListener(this);
btnPhantram.setOnClickListener(this);
btnNhan.setOnClickListener(this);
btnChia.setOnClickListener(this);
btnCong.setOnClickListener(this);
btnTru.setOnClickListener(this);
btnBang.setOnClickListener(this);
btnCham.setOnClickListener(this);
}
public void getValue() {
edNumer = (EditText) findViewById(R.id.edTinh);
tvResult = (TextView) findViewById(R.id.tvKetqua);
btnNum0 = (Button) findViewById(R.id.num0);
btnNum1 = (Button) findViewById(R.id.num1);
btnNum2 = (Button) findViewById(R.id.num2);
btnNum3 = (Button) findViewById(R.id.num3);
btnNum4 = (Button) findViewById(R.id.num4);
btnNum5 = (Button) findViewById(R.id.num5);
btnNum6 = (Button) findViewById(R.id.num6);
btnNum7 = (Button) findViewById(R.id.num7);
btnNum8 = (Button) findViewById(R.id.num8);
btnNum9 = (Button) findViewById(R.id.num9);
btnCong = (Button) findViewById(R.id.cong);
btnTru = (Button) findViewById(R.id.tru);
btnNhan = (Button) findViewById(R.id.nhan);
btnChia = (Button) findViewById(R.id.chia);
btnBang = (Button) findViewById(R.id.bang);
btnPhantram = (Button) findViewById(R.id.phanTram);
btnAc = (Button) findViewById(R.id.ac);
btnC = (Button) findViewById(R.id.c);
btnCham = (Button) findViewById(R.id.cham);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.num0:
edNumer.append("0");
break;
case R.id.num1:
edNumer.append("1");
break;
case R.id.num2:
edNumer.append("2");
break;
case R.id.num3:
edNumer.append("3");
break;
case R.id.num4:
edNumer.append("4");
break;
case R.id.num5:
edNumer.append("5");
break;
case R.id.num6:
edNumer.append("6");
break;
case R.id.num7:
edNumer.append("7");
break;
case R.id.num8:
edNumer.append("8");
break;
case R.id.num9:
edNumer.append("9");
break;
case R.id.cong:
edNumer.append("+");
break;
case R.id.tru:
edNumer.append("-");
break;
case R.id.nhan:
edNumer.append("x");
break;
case R.id.chia:
edNumer.append("/");
break;
case R.id.bang:
Double result = 0d;
addOperation(edNumer.getText().toString());
addNumber(edNumer.getText().toString());
DecimalFormat df = new DecimalFormat("#,##0.###");
if (arrOperation.size() >= arrNumber.size() || arrOperation.size() < 1) {
tvResult.setText("Lỗi định dạng");
} else {
for (int i = 0; i < (arrNumber.size() - 1); i++) {
switch (arrOperation.get(i)) {
case "+":
if (i == 0) {
result = arrNumber.get(i) + arrNumber.get(i + 1);
} else {
result = result + arrNumber.get(i + 1);
}
break;
case "-":
if (i == 0) {
result = arrNumber.get(i) - arrNumber.get(i + 1);
} else {
result = result - arrNumber.get(i + 1);
}
break;
case "x":
if (i == 0) {
result = arrNumber.get(i) * arrNumber.get(i + 1);
} else {
result = result * arrNumber.get(i + 1);
}
break;
case "/":
if (i == 0) {
result = arrNumber.get(i) / arrNumber.get(i + 1);
} else {
result = result / arrNumber.get(i + 1);
}
break;
default:
break;
}
}
tvResult.setText(df.format(result) + "");
}
// Log.d("TAG", result.toString());
break;
case R.id.phanTram:
edNumer.append("%");
break;
case R.id.ac:
edNumer.setText("");
tvResult.setText("");
break;
case R.id.c:
// String numberAfter = deleteNum(tvNumer.getText().toString());
// tvNumer.setText(numberAfter);
BaseInputConnection textInput = new BaseInputConnection(edNumer, true);
textInput.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
break;
case R.id.cham:
edNumer.append(".");
break;
default:
break;
}
}
// public String deleteNum(String num){
// int length = num.length();
// String tem = num.substring(0, length-1);
// return tem;
// }
public ArrayList<String> arrOperation;
public ArrayList<Double> arrNumber;
public int addOperation(String input) {
arrOperation = new ArrayList<>();
char[] cArray = input.toCharArray();
for (int i = 0; i < cArray.length; i++) {
switch (cArray[i]) {
case '+':
arrOperation.add(cArray[i] + "");
break;
case '-':
arrOperation.add(cArray[i] + "");
break;
case 'x':
arrOperation.add(cArray[i] + "");
break;
case '/':
arrOperation.add(cArray[i] + "");
break;
default:
break;
}
}
return 0;
}
public void addNumber(String stringInput) {
arrNumber = new ArrayList<>();
Pattern regex = Pattern.compile("(\\d+(?:\\.\\d+)?)");
Matcher matcher = regex.matcher(stringInput);
while (matcher.find()) {
arrNumber.add(Double.valueOf(matcher.group(1)));
}
}
}
0 nhận xét:
Post a Comment