0% found this document useful (0 votes)
4 views12 pages

Pendahuluan Pemrograman Perangkat Bergerak

This document discusses event handling in Android programming. It defines event handling as the mechanism for how Android handles interactions with the user and components. It describes event listeners as interfaces that contain callback functions like onClick() and onTouch() that are executed when the user interacts with the UI. It provides an example of declaring an event listener using an OnClickListener and setting it for a button. It also includes an example XML layout with a button and code to set an onClick listener for the button that shows a toast message when clicked.

Uploaded by

Java 9507
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Pendahuluan Pemrograman Perangkat Bergerak

This document discusses event handling in Android programming. It defines event handling as the mechanism for how Android handles interactions with the user and components. It describes event listeners as interfaces that contain callback functions like onClick() and onTouch() that are executed when the user interacts with the UI. It provides an example of declaring an event listener using an OnClickListener and setting it for a button. It also includes an example XML layout with a button and code to set an onClick listener for the button that shows a toast message when clicked.

Uploaded by

Java 9507
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PEMROGRAMAN

PERANGKAT BERGERAK
Event Handling

Muarifin
[Link]
muarifin@[Link]
OUTLINE
 Event Listener
 Deklarasi
 Contoh

Muarifin
[Link]
muarifin@[Link]
Event Handling

• Adalah sebuah mekanisme bagaimana Android untuk


menangani interaksi dengan User dengan komponen pada
Android.

• Apa gunanya aplikasi jika tanpa interaksi dengan


penggunanya??
Event Listener

• Adalah interface dari class View yang mengandung fungsi


callback.
• Fungsi ini akan di eksekusi oleh Android Framework ketika ada
interaksi oleh user pada UI. Beberapa fungsi tersebut adalah :
• onClick()
• onLongClick()
• onFocusChange()
• onkey()
• onTouch()
• onCreateContexMenu()
Deklarasi Event Listener

private OnClickListener mCorkyListener = new OnClickListener() {


    public void onClick(View v) {
      // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {


    ...
    // Capture our button from layout
    Button button = (Button)findViewById([Link]);
    // Register the onClick listener with the implementation above
    [Link](mCorkyListener);
    ...
}
Deklarasi Event Listener

public class ExampleActivity extends Activity implements


OnClickListener {
    protected void onCreate(Bundle savedValues) {
        ...
        Button button = (Button)findViewById([Link]);
        [Link](this);
    }

    // Implement the OnClickListener callback


    public void onClick(View v) {
      // do something when the button is clicked
    }
 
Contoh

XML layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
"
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/buttonKu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
Contoh

protected void onCreate(Bundle savedInstanceState) {


[Link](savedInstanceState);
setContentView([Link].activity_main);

Button myButton=(Button) findViewById([Link]);


[Link](buttonKuListener);
}
private [Link] buttonKuListener=new [Link]() {
@Override
public void onClick(View v) {
Toast toast = [Link](getApplicationContext(), "ButtonKu telah di
Klik", Toast.LENGTH_SHORT);
[Link]();
}
};
Contoh Lain
TUGAS
1. Buatlah branch baru “minggu06” untuk implementasi workshop
minggu ini.
2. Lanjutkan aplikasi kalkulator anda. Tambahkan fungsi Event Handling
pada Button. Minimal fungsi: Tambah, Kurang, Kali, dan Bagi. Lihat
video di slide referensi untuk mempelajari cara pembuatannya.
3. Pastikan ada folder screenshot yang didalamnya ada folder minggu06
yang berisi hasil pekerjaan minggu ini.
4. Release versi 6.0 jika sudah selesai.
TAMBAHAN NILAI = (1) Fungsi operator yang lainnya, (2) Koma Angkat,
(3) BODMAS RULE
Referensi
1. [Link]
2. [Link]

Muarifin
[Link]
muarifin@[Link]
TERIMAKASIH

Muarifin
[Link]
muarifin@[Link]

You might also like