Membuat CheckBox di Android Studio
01.50kali ini saya akan menjelaskan bagaimana cara membuat CheckBox di Android Studio
dimana langkah pertama yakni, kita akan membuata project baru, di Android Apllication Project setelah itu beri nama project, disini saya beri nama project dengan CheckBox
setelah selesai maka kita akan menambahkan script pada MainActivity.java tambahkan script berikut
package checkbox.checkbox;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class Checkbox extends Activity {
CheckBox cb1, cb2, cb3, cb4, cb5;
Button tombol;
String tampil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox);
cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb2 = (CheckBox) findViewById(R.id.checkBox2);
cb3 = (CheckBox) findViewById(R.id.checkBox3);
cb4 = (CheckBox) findViewById(R.id.checkBox4);
cb5 = (CheckBox) findViewById(R.id.checkBox5);
tombol = (Button) findViewById(R.id.button1);
//membuat listener ketika dilakukan klik pada objek
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
if (cb1.isChecked()) tampil += cb1.getText().toString();
if (cb2.isChecked()) tampil += ", " + cb2.getText().toString();
if (cb3.isChecked()) tampil += ", " + cb3.getText().toString();
if (cb4.isChecked()) tampil += ", " + cb4.getText().toString();
if (cb5.isChecked()) tampil += ", " + cb5.getText().toString();
Toast.makeText(Checkbox.this,
"Hobi anda adalah " + tampil,
Toast.LENGTH_SHORT).show();
tampil = ""; //kosongkan variabel tampil
}
};
//menerapkan listener pada tombol
tombol.setOnClickListener(listener);
}
}
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="46dp"
android:layout_marginTop="48dp"
android:text="Pilih hobi kamu!"
android:textSize="20sp" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="22dp"
android:text="Olah raga" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_below="@+id/checkBox1"
android:text="Membaca" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox2"
android:layout_below="@+id/checkBox2"
android:text="Menulis" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox3"
android:layout_centerVertical="true"
android:text="Travelling" />
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox4"
android:layout_below="@+id/checkBox4"
android:text="Lainnya" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox5"
android:layout_below="@+id/checkBox5"
android:layout_marginTop="17dp"
android:text="Kirim" />
</RelativeLayout>
0 komentar