بسم الله الرحمن الرحيم
إن شاء الله في هذه التدوينة لا تخرج إلا وتعلمت كيفية برمجة آلة حاسبة مبسطة على العمليات الأربعة, الجمع والطرح والقسمة والضرب.
لقد سجلت لكم هذا الشرح على جزئين واحد للهيكل .xml والثاني الجزء البرمجي .java
الأكواد بعد انتهائي من التسجيل :
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package com.andrody.testandrodycom; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { EditText T1, T2; TextView V1, V2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); T1 = (EditText) findViewById(R.id.Text1); T2 = (EditText) findViewById(R.id.Text2); V1 = (TextView) findViewById(R.id.sign); V2 = (TextView) findViewById(R.id.View0); } public void onClick(View v) { float number1, number2; float result = 0; if (TextUtils.isEmpty(T1.getText().toString()) || TextUtils.isEmpty(T2.getText().toString())) { return; } number1 = Float.parseFloat(T1.getText().toString()); number2 = Float.parseFloat(T2.getText().toString()); switch (v.getId()) { case R.id.on1: result = number1 + number2; V1.setText("+"); break; case R.id.on2: result = number1 - number2; V1.setText("-"); break; case R.id.on3: result = number1 / number2; V1.setText("/"); break; case R.id.on4: result = number1 * number2; V1.setText("×"); break; case R.id.on5: T1.setText(""); T2.setText(""); V1.setText("."); break; } V2.setText("" + result + ""); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="15dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_weight="4"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="تعلم برمجة آلة حاسبة مبسطة على اندرويد" android:textColor="#ff20200a" android:textSize="25sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="بواسطة اندرودي عربي" android:textColor="#ff530610" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" android:layout_weight="3"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="numberDecimal" android:ems="10" android:layout_weight="1" android:id="@+id/Text1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="." android:background="#000" android:padding="5dp" android:textColor="#fff" android:id="@+id/sign" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="numberDecimal" android:ems="10" android:layout_weight="1" android:id="@+id/Text2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" = " /> <TextView android:layout_width="100dp" android:gravity="center" android:layout_height="wrap_content" android:text="" android:background="#000" android:padding="5dp" android:textColor="#fff" android:id="@+id/View0" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#000" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="جمع" android:id="@+id/on1" android:onClick="onClick" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="طرح" android:id="@+id/on2" android:onClick="onClick" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="قسمة" android:id="@+id/on3" android:onClick="onClick" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ضرب" android:id="@+id/on4" android:onClick="onClick" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/on5" android:onClick="onClick" android:text="تصفير العداد" /> </LinearLayout> </LinearLayout> |
1 |
<activity android:name=".NAME_AVTIVITY" android:screenOrientation="landscape"/> |
# متابعة الجزء الثاني ( البرمجة ):
أتمنى لكم متابعة مفيدة. وانتظروني أصدقائي بكل جديد وحصري ومميز .. وفي أمان الله 🙂
يعطيك العافية ،،
بس عندي سؤال ،، كيف لو ابدي مربع النص للقيم المراد عمل العمليات عليها مربع واحد وليس مربعين ؟
ربي يعافيك .. واعتذر عن تاخري بالرد اخي الفاضل ..
يوجد امثلة كثيرة تساعدك في ذلك منها : http://mrbool.com/how-to-create-a-calculator-app-for-android/28100
إذا لم يعمل معك .. اخبرني لتجهيز مثال بسيطة لذلك 🙂
كيف انسخ الاكواد ~~” ؟
اضغط على الكود .. يوجد في زاوية الكود علامة صغيرة لفتح النص في نافذة صغيرة.
كيف اعمل حاسبة المعدل GPA