Demo RadioButton [Android]

Project yang sekarang menjelaskan tentang bagaimana membuat RadioButton pada Android. Project kali ini akan membuat 3 RadioButton, yang jika di pilih salah satunya, akan mengganti warna background pada TextView yang ada di bawahnya. Berikut contoh programnya:

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Demo RadioButton</string>
<string name="btnText">Exit</string>
<string name="strTxt">DYAH FAJAR NUR ROHMAH</string>
<drawable name="bgRed">#FF0000</drawable>
<drawable name="bgGreen">#00FF00</drawable>
<drawable name="bgBlue">#0000FF</drawable>
</resources>



main.xml
<?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"
    android:gravity="center_horizontal">

<RadioGroup android:id="@+id/RadioGroup01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<RadioButton 
android:text="Red" 
android:id="@+id/rdb1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</RadioButton>
<RadioButton 
android:text="Green" 
android:id="@+id/rdb2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</RadioButton>
<RadioButton 
android:text="Blue" 
android:id="@+id/rdb3" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</RadioButton>

</RadioGroup>
<TextView 
android:text="@string/strTxt" 
android:id="@+id/txtView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:background="#000000">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnProses"
android:text="@string/btnText">
</Button>
</LinearLayout>

Untuk class activitynya, untuk project ini diberi nama RdActivity.java:

package com.latihan;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class RdActivity extends Activity implements OnClickListener{
    RadioButton rd1;
    RadioButton rd2;
    RadioButton rd3;
    TextView txt;
    Button btn;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        rd1 = (RadioButton) findViewById(R.id.rdb1);
        rd2 = (RadioButton) findViewById(R.id.rdb2);
        rd3 = (RadioButton) findViewById(R.id.rdb3);
        txt = (TextView) findViewById(R.id.txtView);
        btn = (Button) findViewById(R.id.btnProses);
        


        rd2.setOnClickListener(this);
        rd3.setOnClickListener(this);
        btn.setOnClickListener(this);
        
    }
    @Override
    public void onClick(View v) {
        if (v == btn){
            finish();
        }else if (v == rd1){
            txt.setBackgroundResource(R.drawable.bgRed);
        } else if (v == rd2){
            txt.setBackgroundResource(R.drawable.bgGreen);
        } else if (v == rd3){
            txt.setBackgroundResource(R.drawable.bgBlue);
        }
    }
}

Komentar

  1. ga, pake apa nie buatnya, pake netbeans g?
    ato ada android developer sendiri?
    ehm kalo blm pnya hp android g bisa coba dunk......iya ga?

    BalasHapus
    Balasan
    1. ini bisa pakek Eclipse ama ADK bisa download and nyoba langsung...

      setauq ini systemnya WYSIWYG so apa yang ada di emulator ya kemungkinan seperti pula yang ada di handset CMIIW D

      Hapus

Posting Komentar

Terima kasih sudah membaca....^^