يمكن تهيئة الاندرويد ستديو ايا كانت النسخة الذي تعمل بها عن طريق plugin يتم اضافتها له
او عن طريق تحميل اخر نسخة من برنامج اندوريد ستديو من الموقع الرسمي Android Studio
هذه النسخة لم تزل تجريبية نسخة وتمت تسميتها Canary
يمكنك تحميلها او اتباع الخطوات التالي لتهيئة النسخة الحالية لديك
-
قم ببناء مشروع جديد Start a new project > name TestKotlin >Minimum SDK 19 >Empty Activity and press Finish
ثانس شيئ سوف نحتاجه وهوا اضافة لغة Kotlin الى برنامج android studio اضغط File>Settings>Plugins>Install JetBrains plugin وقم في البحث عن kotlin وحمل الاضافة عند انتهاء التحميل سوف يطلب اعادة تشغيل البرنامج قم باعادة تشغيله
الان نقم باضافة كوتلين الى المشروع اضغط على Tools>Kotlin>Configure Kotlin In Project > Android With gradle واختر Sync Now
عند الانتهاء قم بفتح ملف جافا واضغط على Code>Convert Java File To Kotlin File
وبهذه الخطو نكون انتهينا من اضافة اللغة واصبح المشروع جاهز لبداء العمل مع للغة كوتلين
الان سوف نقوم بانشاء الة حاسبة بسيطة
كود ملف MainActivity
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 |
package com.izzedineeita.testkotlin import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import android.widget.EditText import android.widget.TextView class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val buttonPlus = findViewById(R.id.button_plus) as Button val buttonMinus = findViewById(R.id.button_minus) as Button val buttonMulti = findViewById(R.id.button_multi) as Button val result = findViewById(R.id.textView_result) as TextView buttonPlus.setOnClickListener(View.OnClickListener { v -> result.text = (getNumber1() + getNumber2()).toString() }) buttonMinus.setOnClickListener(View.OnClickListener { view -> result.text = (getNumber1() - getNumber2()).toString() }) buttonMulti.setOnClickListener(View.OnClickListener { view -> result.text = (getNumber1() * getNumber2()).toString() }) } fun getNumber1(): Int { val number = findViewById(R.id.editText1) as EditText return Integer.parseInt(number.text.toString()) } fun getNumber2(): Int { val number1 = findViewById(R.id.editText2) as EditText return Integer.parseInt(number1.text.toString()) } } |
1 |
val buttonPlus = findViewById(R.id.button_plus) as Button |
الكود في هنى لربط العنصر الموجود في الواجهة برمجياً في ملف ال kotlin وتحكم به
المتغير هنى val بمعنى انه متغير ثابت لا يمكن تغير محتواه مثل final في ال java
1 2 3 4 |
fun getNumber1(): Int { val number = findViewById(R.id.editText1) as EditText return Integer.parseInt(number.text.toString()) } |
هذه ال method هنى هي لجلب قيمة editText من واجهة التطبيق وتحويلها الى متحول رقمي integer لنستطيع التعامل معها في عمليات الجمع والضرب
1 2 3 |
buttonPlus.setOnClickListener(View.OnClickListener { v -> result.text = (getNumber1() + getNumber2()).toString() }) |
هنى ال event الخاص في button نقوم بعملية الجمع داخله وعرض القيمة داخل textView في واجهة التطبيق
1 |
result.text = (getNumber1() + getNumber2()).toString |
هنى نقوم بعملية الجمع واظهار النتيجة result هوا متغير textview في ال java كنى نقوم بكتابة
result.setText();
اما في كوتلين فقط بكتابة اسم المتغير وكتابة text واعطائه القيمة
كود ملف 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 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context="com.izzedineeita.testkotlin.MainActivity"> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="number" tools:layout_editor_absoluteX="83dp" tools:layout_editor_absoluteY="1dp" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="number" tools:layout_editor_absoluteX="83dp" tools:layout_editor_absoluteY="85dp" /> <Button android:id="@+id/button_plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" tools:layout_editor_absoluteX="38dp" tools:layout_editor_absoluteY="175dp" /> <Button android:id="@+id/button_minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" tools:layout_editor_absoluteX="147dp" tools:layout_editor_absoluteY="175dp" /> <Button android:id="@+id/button_multi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" tools:layout_editor_absoluteX="257dp" tools:layout_editor_absoluteY="175dp" /> <TextView android:id="@+id/textView_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textStyle="bold" android:text="result"/> </LinearLayout> |
Permalink
Permalink