Selamat datang belajar bareng SMK TI

Cara mebuat CheckBox pada eclips

Sunday, October 6, 20130 comments

Kadang kita membutuhkan sebuah checkBox untuk memilih beberapa pilihan. Tutorial kali ini kita akan berlatih menggunakan widget CheckBox, ketika checkbox aktif tulisan berbunyi “checkBox ini: Dicentang!” dan saat checkBox tidak aktif tulisan berbunyi :checkBox ini:
Tidak dicentang!”.

1. Jalankan Enclipse, buat Project baru.
2. Isilah parameter seperti berikut

            Project nameMembuatCheckBox
            ContentsCreate new project in workspace
            Build TargetAndroid 2.1
            Application nameMembuat Check Box
            Package namecontoh.checkBox
            Create ActivitycheckBox
            Min SDK version7

3. Perhatikan kode pada String.xml. (res/values/string.xml). Tambahkan kode menjadi
   seperti berikut


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World,
checkBox!</string>
    <string name="app_name">Membuat Check Box</string>
    <string name="checkBox">checkBox ini : Tidak
Dicentang!</string>
</resources>

4. Kemudian ketikkan kode berikut ini pada Main.xml.

<?xml version="1.0" encoding="utf-8"?>
    <CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
     android:id="@+id/check"
android:layout_height="wrap_content"
android:text="@string/checkBox">
</CheckBox>

5. Tuliskan kode CheckBox.java seperti berikut

package contoh.checkBox;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import
android.widget.CompoundButton.OnCheckedChangeListener;
public class checkBox extends Activity implements
OnCheckedChangeListener {
      CheckBox cb;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
cb=(CheckBox)findViewById(R.id.check);
cb.setOnCheckedChangeListener(this);
    }
    public void onCheckedChanged(CompoundButton
buttonView,
            boolean isChecked) {
            if (isChecked) {
            cb.setText("checkBox ini : Dicentang!");
            }
            else {
            cb.setText("checkBox ini : Tidak
Dicentang!");
            }
            }
}

6. Bila kode berantakan, lakukan Format (source > format).
7. Lakukan RUN dan lihat hasilnya. Mudah bukan???
8. Hasilnya akan tampil seperti berikut ini


Share this article :

Post a Comment

 
Support : SMK TI AN-Najiyah | Jasa buat blog
Copyright © 2011. belajar bersama - All Rights Reserved
Template Created by Creating Website Modify by CaraGampang
Proudly powered by Blogger