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 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:text="@string/pilihan" android:textSize="20sp" /> <RadioGroup android:id="@+id/rBtnGrp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/gorengRBtn" android:text="@string/pilihan1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/udukRBtn" android:text="@string/pilihan2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioButton android:id="@+id/pecelRBtn" android:text="@string/pilihan3" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RadioGroup> </LinearLayout> |
Kemudian pada folder res/value/strings.xml :
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">RadioButton01</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 Uduk</string> <string name="pilihan3">Nasi Pecel</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 | package com.example.radiobutton01; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.RadioButton; import android.widget.RadioGroup; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup rdgrp = (RadioGroup)findViewById(R.id.rBtnGrp); RadioButton newRadioBtn = new RadioButton(this); newRadioBtn.setText("Nasi Kuning"); rdgrp.addView(newRadioBtn); newRadioBtn.setChecked(true); } @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