Thursday, May 4, 2017

[MOBILE PROGRAMMING] WIDGET VIEW - RadioButton02

Berikut adalah potongan kode dalam bentuk XML:
 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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
 
    <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" />
 
    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
         <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rbbaso"
            android:text="@string/pilihan1"
            />
        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rbMie"
            android:text="@string/pilihan2"
            />
      </RadioGroup>
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center|bottom"
        android:text="@string/produk"
        android:textColor="#2ecc71"
        android:textSize="26sp" />
 
 
</LinearLayout>


Kemudian pada folder res/value/strings.xml :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Contoh RadioButton</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="judul">Contoh Radio Button Android</string>
    <string name="pilihan">Pilih Makan Favorit</string>
    <string name="pilihan1">Baso Tahu</string>
    <string name="pilihan2">Mie Ayam</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
package com.example.radiobutton02;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity implements OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //RadioButton rb1,rb2;
        RadioGroup rg;
        
        rg = (RadioGroup)findViewById(R.id.rg);
        //rb1 = (RadioButton)findViewById(R.id.rbbaso);
        //rb2 = (RadioButton)findViewById(R.id.rbMie);
        rg.setOnCheckedChangeListener(this);
    }

    public void onCheckedChanged(RadioGroup group, int checkedId) {
     if(checkedId==R.id.rbbaso)
     {
      Toast.makeText(this, "Anda Membeli Baso Tahu",Toast.LENGTH_SHORT).show();
     }
     if(checkedId==R.id.rbMie)
     {
      Toast.makeText(this, "Anda Membeli Mie Ayam", 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