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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | <?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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:text="@string/judul" android:textSize="20sp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:text="@string/pilihan" android:textSize="15sp" /> <CheckBox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pilihan1"/> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pilihan2" android:checked="false" /> <CheckBox android:id="@+id/cb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pilihan3"/> <CheckBox android:id="@+id/cb4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pilihan4" android:checked="false" /> <CheckBox android:id="@+id/cb5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pilihan5" android:checked="false" /> <CheckBox android:id="@+id/cb6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pilihan6" android:checked="false" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tampil" android:id="@+id/btn1" android:layout_gravity="center_horizontal" android:onClick="toast"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center|bottom" android:text="@string/produk" android:textColor="#2ecc71" android:textSize="20sp" /> </LinearLayout> |
Kemudian pada folder res/value/strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Contoh CheckBox</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="judul">Contoh CheckBox Android</string> <string name="pilihan">Pilih Bahasa Pemrograman</string> <string name="pilihan1">Java</string> <string name="pilihan2">PHP</string> <string name="pilihan3">C++</string> <string name="pilihan4">Actionscript</string> <string name="pilihan5">Phyton</string> <string name="pilihan6">Javascript</string> <string name="tampil">Tampil</string> <string name="produk">::. Punisoft Integrated .::</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 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 60 61 62 63 64 65 66 67 68 69 | package com.example.checkbox02; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.CheckBox; import android.widget.Toast; public class MainActivity extends Activity { CheckBox cb1, cb2,cb3,cb4,cb5,cb6; //Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cb1 = (CheckBox)findViewById(R.id.cb1); cb2 = (CheckBox)findViewById(R.id.cb2); cb3 = (CheckBox)findViewById(R.id.cb3); cb4 = (CheckBox)findViewById(R.id.cb4); cb5 = (CheckBox)findViewById(R.id.cb5); cb6 = (CheckBox)findViewById(R.id.cb6); //btn1 = (Button)findViewById(R.id.btn1); //btn1.setOnClickListener(this); } public void toast(View v){ String a=""; if(cb1.isChecked()){ a+="Bahasa Java\n"; } if(cb2.isChecked()){ a+="Bahasa PHP\n"; } if(cb3.isChecked()){ a+="Bahasa C++\n"; } if(cb4.isChecked()){ a+="Bahasa Actionscript\n"; } if(cb5.isChecked()){ a+="Bahasa Phyton\n"; } if(cb6.isChecked()){ a+="Bahasa Javascript\n"; } Toast.makeText(this, "Kalian memilih : \n "+a,Toast.LENGTH_SHORT).show(); } @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