Minggu, 01 Maret 2015

Materi Pertemuan 3

Buat Layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textNama"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Nama Lengkap"
                android:textSize="24dp"
                android:textStyle="bold"
                android:typeface="monospace" />

            <EditText
                android:id="@+id/editNama"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="24dp"
                android:typeface="monospace" />

            <TextView
                android:id="@+id/textEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Email"
                android:textSize="24dp"
                android:textStyle="bold"
                android:typeface="monospace" />

            <EditText
                android:id="@+id/editEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:textSize="24dp"
                android:typeface="monospace" >

                <requestFocus />
            </EditText>

            <TextView
                android:id="@+id/textJenisKelamin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Jenis Kelamin"
                android:textSize="24dp"
                android:textStyle="bold"
                android:typeface="monospace" />

            <RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RadioButton
                    android:id="@+id/radioLaki"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="Laki-Laki" />

                <RadioButton
                    android:id="@+id/radioPerempuan"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Perempuan" />
            </RadioGroup>

            <TextView
                android:id="@+id/textHobi"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hobi"
                android:textSize="24dp"
                android:textStyle="bold"
                android:typeface="monospace" />

            <CheckBox
                android:id="@+id/checkBaca"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Membaca" />

            <CheckBox
                android:id="@+id/checkMemancing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Memancing" />

            <CheckBox
                android:id="@+id/checkBersepeda"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bersepeda" />

            <CheckBox
                android:id="@+id/checkMusik"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Bermusik" />

            <CheckBox
                android:id="@+id/checkOlahraga"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Olah Raga" />

            <CheckBox
                android:id="@+id/checkBelajar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Belajar" />

            <CheckBox
                android:id="@+id/checkBerbelanja"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Belanja" />

            <CheckBox
                android:id="@+id/checkBerwisata"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Wisata" />

            <CheckBox
                android:id="@+id/checkBerkemah"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Berkemah" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="5"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/btnOK"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="OK" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="5"
            android:gravity="center_horizontal" >

            <TextView
                android:id="@+id/textMessage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>


Buat Program Javanya
package com.example.widget;

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

public class MainActivity extends Activity{
    EditText editNama;
    EditText editEmail;
    RadioButton radioLaki;
    RadioButton radioPerempuan;
    CheckBox checkBaca;
    CheckBox checkMemancing;
    CheckBox checkBersepeda;
    CheckBox checkMusik;
    CheckBox checkOlahraga;
    CheckBox checkBelajar;
    CheckBox checkBerbelanja;
    CheckBox checkBerwisata;
    CheckBox checkBerkemah;  
    Button btnOK;
    TextView txtPilihan;
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editNama = (EditText) findViewById(R.id.editNama);
        editEmail = (EditText) findViewById(R.id.editEmail);
        radioLaki = (RadioButton) findViewById(R.id.radioLaki);
        radioPerempuan = (RadioButton) findViewById(R.id.radioPerempuan);
        checkBaca = (CheckBox) findViewById(R.id.checkBaca);
        checkMemancing = (CheckBox) findViewById(R.id.checkMemancing);
        checkBersepeda = (CheckBox) findViewById(R.id.checkBersepeda);
        checkMusik = (CheckBox) findViewById(R.id.checkMusik);
        checkOlahraga = (CheckBox) findViewById(R.id.checkOlahraga);
        checkBelajar = (CheckBox) findViewById(R.id.checkBelajar);
        checkBerbelanja = (CheckBox) findViewById(R.id.checkBerbelanja);
        checkBerwisata = (CheckBox) findViewById(R.id.checkBerwisata);
        checkBerkemah = (CheckBox) findViewById(R.id.checkBerkemah);
        btnOK = (Button) findViewById(R.id.btnOK);
        txtPilihan = (TextView) findViewById(R.id.textMessage);
        btnOK.setOnClickListener(new klik());
    }
    class klik implements OnClickListener{
        @Override
        public void onClick(View v){
            String pilihan = editNama.getText().toString() + " " + editEmail.getText().toString() + " ";
            if(radioLaki.isChecked())
                pilihan = pilihan + radioLaki.getText() + " ";
            if(radioPerempuan.isChecked())
                pilihan = pilihan + radioPerempuan.getText() + " ";
            if(checkBaca.isChecked())
                pilihan = pilihan + checkBaca.getText() + " ";
            if(checkMemancing.isChecked())
                pilihan = pilihan + checkMemancing.getText() + " ";
            if(checkBersepeda.isChecked())
                pilihan = pilihan + checkBersepeda.getText() + " ";
            if(checkMusik.isChecked())
                pilihan = pilihan + checkMusik.getText() + " ";
            if(checkOlahraga.isChecked())
                pilihan = pilihan + checkOlahraga.getText() + " ";
            if(checkBelajar.isChecked())
                pilihan = pilihan + checkBelajar.getText() + " ";
            if(checkBerbelanja.isChecked())
                pilihan = pilihan + checkBerbelanja.getText() + " ";
            if(checkBerwisata.isChecked())
                pilihan = pilihan + checkBerwisata.getText() + " ";
            if(checkBerkemah.isChecked())
                pilihan = pilihan + checkBerkemah.getText() + " ";          
            txtPilihan.append("Pilihan Anda adalah: " +pilihan);
        }      
    }
}

 

Tidak ada komentar:

Posting Komentar