Source Code Cara membuat RadioButton dengan Android



/* Activity_button.java */
package com.membuatradiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class ButtonActivity extends  Activity implements
      RadioGroup.OnCheckedChangeListener {
      /** Pemanggilan activity pertama */
            RadioGroup orientation; RadioGroup gravity;
     
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_button);
            orientation = (RadioGroup)
            findViewById(R.id.orientation);
                        orientation.setOnCheckedChangeListener((OnCheckedChangeListener) this);
                        RadioGroup gravity = (RadioGroup)
                        findViewById(R.id.gravity);
                        gravity.setOnCheckedChangeListener((OnCheckedChangeListener) this);
                        }
      public void onCheckedChanged(RadioGroup group, int
                  checkId) {
                  switch (checkId) {
                  case R.id.horizontal:
                  orientation.setOrientation(LinearLayout.HORIZONTAL);
                  break;
                  case R.id.vertical:
                  orientation.setOrientation(LinearLayout.VERTICAL);
                  break;
                  case R.id.kiri:
                  gravity.setGravity(Gravity.LEFT);
                  break;
                  case R.id.tengah:
                  gravity.setGravity(Gravity.CENTER);
                  break;
                  case R.id.kanan:
                  gravity.setGravity(Gravity.RIGHT);
                  break;
                  }
            }
      }

/* Button_activity.xml */
 

    
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">
    <RadioGroup
        android:padding="5px"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/orientation"
            android:orientation="horizontal">
            <RadioButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:id="@+id/horizontal"
                android:text="Horizontal">
             </RadioButton>
             <RadioButton
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:id="@+id/vertical"
                android:text="Vertical">
             </RadioButton>
             </RadioGroup>
             <RadioGroup
                 android:id="@+id/gravity"
                 android:orientation="vertical"
                 android:padding="5px"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content">
                 <RadioButton
                     android:id="@+id/kanan"
                     android:text="Kanan">
                  </RadioButton>
                  <RadioButton
                     android:text="Kiri"
                     android:id="@+id/kiri">
                  </RadioButton>
                  <RadioButton
                     android:id="@+id/tengah"
                     android:text="Tengah">
                     </RadioButton>
            </RadioGroup>
</LinearLayout>

0 comments:

Post a Comment