Wednesday, May 3, 2017

[MOBILE PROGRAMMING] WIDGET VIEW - Check Box 01

CheckBox memiliki dua keadaan yaitu On dan Off sama seperti ToggleButton. Dalam pembuatannya di Android, dapat menggunakan class android.widget.CheckBox. Berikut adalah potongan kode dalam bentuk 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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Pilihan" />
    <CheckBox
        android:text="@string/Pilihan1" 
        android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <CheckBox
     android:text="@string/Pilihan2" 
     android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <CheckBox
     android:text="@string/Pilihan3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <CheckBox
     android:text="@string/Pilihan4" 
     android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

</LinearLayout>


Kemudian pada folder res/value/strings.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CheckBox01</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="Pilihan">Makanan Kesukaan :</string>
    <string name="Pilihan1">Nasi Goreng</string>
    <string name="Pilihan2">Nasi Pecel</string><string name="Pilihan3">Nasi Uduk</string>
    
    <string name="Pilihan4">Nasi Kuning</string>

</resources>


Berikut ini adalah kode menggunakan code Java:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.checkbox01;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}


Hasil potongan kode di atas adalah sebagai berikut:

No comments:

Post a Comment