ویرگول
ورودثبت نام
مهدی ام
مهدی امبرنامه نویس اندروید هستم و میخوام با پروژه های کوچیک چیزایی که از اندروید بلدم رو یاد بدم :)
مهدی ام
مهدی ام
خواندن ۷ دقیقه·۶ سال پیش

ماشین حساب خیلیییی ساده - اندروید

android studio
android studio


سلام ان شا الله که حال دلتون خوب باشه

تصمیم دارم جلسات انتقال تجربه ی خودمو که تو دانشگاه در قالب ۱۰ جلسه به صورت پروژه های کوچیک قرار بود انجام بدم رو اینجا هم بزارم تا شاید کمکی کرده باشم. قراره از کارای خیلی ساده شروع کنم و به یه سطح معمولی برسیم.

این پروژه خیلی ساده و کوچیکه. ۴ تا عمل اصلی رو داره ولی قراره یه ماشین حساب خوشگل تر هم درست کنیم. پس اینو میزارم اینجا تا چند روز بعد یه ماشین حساب خوشگل تر هم بهش اضافه بشه.

تصمیم دارم جلسات انتقال تجربه ی خودمو که تو دانشگاه در قالب ۱۰ جلسه به صورت پروژه های کوچیک قرار بود انجام بدم رو اینجا هم بزارم تا شاید کمکی کرده باشم. قراره از کارای خیلی ساده شروع کنم و به یه سطح معمولی برسیم

در ضمن میتونین سورس کد رو از گیت هاب بگیرین

https://github.com/mehdii08/MySimpleCalculator

فایل اول activity_main.xml هست:

<?xml version=&quot1.0&quot encoding=&quotutf-8&quot?> <LinearLayout xmlns:android=&quothttp://schemas.android.com/apk/res/android&quot xmlns:tools=&quothttp://schemas.android.com/tools&quot android:layout_width=&quotmatch_parent&quot android:layout_height=&quotmatch_parent&quot android:gravity=&quotcenter&quot android:orientation=&quotvertical&quot tools:context=&quot.MainActivity&quot> <LinearLayout android:layout_width=&quotwrap_content&quot android:layout_height=&quotwrap_content&quot android:orientation=&quothorizontal&quot> <EditText android:id=&quot@+id/operand1&quot android:layout_width=&quotwrap_content&quot android:layout_height=&quotwrap_content&quot android:hint=&quotop1&quot android:inputType=&quotnumber&quot android:textSize=&quot30sp&quot /> <TextView android:id=&quot@+id/operator&quot android:layout_width=&quotwrap_content&quot android:layout_height=&quotwrap_content&quot android:hint=&quot+&quot android:textSize=&quot30sp&quot /> <EditText android:id=&quot@+id/operand2&quot android:layout_width=&quotwrap_content&quot android:layout_height=&quotwrap_content&quot android:hint=&quotop2&quot android:inputType=&quotnumber&quot android:textSize=&quot30sp&quot /> <TextView android:layout_width=&quotwrap_content&quot android:layout_height=&quotwrap_content&quot android:hint=&quot=&quot android:textSize=&quot30sp&quot /> <TextView android:id=&quot@+id/result&quot android:layout_width=&quotwrap_content&quot android:layout_height=&quotwrap_content&quot android:hint=&quotresult&quot android:textSize=&quot30sp&quot /> </LinearLayout> <LinearLayout android:layout_width=&quotwrap_content&quot android:layout_height=&quotwrap_content&quot android:layout_marginTop=&quot30sp&quot android:orientation=&quothorizontal&quot> <androidx.appcompat.widget.AppCompatButton android:id=&quot@+id/sum&quot android:layout_width=&quot80sp&quot android:layout_height=&quot80sp&quot android:layout_marginRight=&quot20sp&quot android:=&quotsum&quot android:text=&quot+&quot android:textSize=&quot20sp&quot /> <androidx.appcompat.widget.AppCompatButton android:id=&quot@+id/subtraction&quot android:layout_width=&quot80sp&quot android:layout_height=&quot80sp&quot android:=&quotsubtraction&quot android:text=&quot-&quot android:textSize=&quot20sp&quot /> </LinearLayout> <LinearLayout android:layout_width=&quotwrap_content&quot android:layout_height=&quotwrap_content&quot android:layout_marginTop=&quot20sp&quot android:orientation=&quothorizontal&quot> <androidx.appcompat.widget.AppCompatButton android:id=&quot@+id/multiplication&quot android:layout_width=&quot80sp&quot android:layout_height=&quot80sp&quot android:layout_marginRight=&quot20sp&quot android:=&quotmultiplication&quot android:text=&quot*&quot android:textSize=&quot20sp&quot /> <androidx.appcompat.widget.AppCompatButton android:id=&quot@+id/division&quot android:layout_width=&quot80sp&quot android:layout_height=&quot80sp&quot android:=&quotdivision&quot android:text=&quot/&quot android:textSize=&quot20sp&quot /> </LinearLayout> </LinearLayout>

فایل بعدی MainActivity.java هست:

import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.AppCompatButton; import android.media.Image; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText operand1, operand2; private TextView operator, result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { operand1 = findViewById(R.id.operand1); operand2 = findViewById(R.id.operand2); operator = findViewById(R.id.operator); result = findViewById(R.id.result); } public void sum(View view) { if (operand1.getText().toString().isEmpty()) { Toast.makeText(this, &quotop1 is empty&quot, Toast.LENGTH_SHORT).show(); } else if (operand2.getText().toString().isEmpty()) { Toast.makeText(this, &quotop2 is empty&quot, Toast.LENGTH_SHORT).show(); } else { int a = Integer.parseInt(operand1.getText().toString()); int b = Integer.parseInt(operand2.getText().toString()); int result = a + b; this.operator.setText(&quot+&quot); this.result.setText(String.valueOf(result)); } } public void subtraction(View view) { if (operand1.getText().toString().isEmpty()) { Toast.makeText(this, &quotop1 is empty&quot, Toast.LENGTH_SHORT).show(); } else if (operand2.getText().toString().isEmpty()) { Toast.makeText(this, &quotop2 is empty&quot, Toast.LENGTH_SHORT).show(); } else { int a = Integer.parseInt(operand1.getText().toString()); int b = Integer.parseInt(operand2.getText().toString()); int result = a - b; this.operator.setText(&quot-&quot); this.result.setText(String.valueOf(result)); } } public void multiplication(View view) { if (operand1.getText().toString().isEmpty()) { Toast.makeText(this, &quotop1 is empty&quot, Toast.LENGTH_SHORT).show(); } else if (operand2.getText().toString().isEmpty()) { Toast.makeText(this, &quotop2 is empty&quot, Toast.LENGTH_SHORT).show(); } else { int a = Integer.parseInt(operand1.getText().toString()); int b = Integer.parseInt(operand2.getText().toString()); int result = a * b; this.operator.setText(&quot*&quot); this.result.setText(String.valueOf(result)); } } public void division(View view) { if (operand1.getText().toString().isEmpty()) { Toast.makeText(this, &quotop1 is empty&quot, Toast.LENGTH_SHORT).show(); } else if (operand2.getText().toString().isEmpty()) { Toast.makeText(this, &quotop2 is empty&quot, Toast.LENGTH_SHORT).show(); } else if (Integer.parseInt(operand2.getText().toString()) == 0) { Toast.makeText(this, &quotdivision by zero&quot, Toast.LENGTH_SHORT).show(); } else { int a = Integer.parseInt(operand1.getText().toString()); int b = Integer.parseInt(operand2.getText().toString()); int result = a / b; this.operator.setText(&quot/&quot); this.result.setText(String.valueOf(result)); } } }

خب حالا میتونین اجراش کنین. خروجی مثل تصویر پایین میشه

simple calculator
simple calculator


میدونم خیلی سادس ولی این برای اون دوستانی هست که دفعات اولشونه با اندروید استودیو کار میکنن. ان شا الله تو پستای بعدی پروژه های پیچیده تری رو هم با هم کار میکنیم.

androidcodeprogramming
۲
۰
مهدی ام
مهدی ام
برنامه نویس اندروید هستم و میخوام با پروژه های کوچیک چیزایی که از اندروید بلدم رو یاد بدم :)
شاید از این پست‌ها خوشتان بیاید