Di Android Final Project Part 6, saya akan membahas koding membuat quiz sederhana di android, oke tanpa panjang lebar langsung aja kita ke kodingnya :
1. Buat file start.xml
kodingnya kaya gini :
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backgroundplay"
android:gravity="center" >
<ImageView
android:id="@+id/GambarSoal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:layout_centerHorizontal="true"
android:layout_marginTop="20px"
android:src="@drawable/soal1" >
</ImageView>
<EditText
android:id="@+id/jawabannya"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/GambarSoal"
android:layout_marginTop="20px"
android:text="" >
</EditText>
<Button
android:id="@+id/jawab"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@+id/jawabannya"
android:layout_centerHorizontal="true"
android:background="@drawable/btnanswer" />
<Button
android:id="@+id/next"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/jawab"
android:layout_below="@+id/jawab"
android:layout_marginTop="16dp"
android:background="@drawable/btnnextplay" />
<TextView
android:id="@+id/salahbenar"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/GambarSoal"
android:layout_below="@+id/next"
android:layout_marginTop="46dp"
android:background="@drawable/btnhasil"
android:text="salahbenar"
android:textColor="#ffffff"
android:textSize="70px" />
</RelativeLayout>
2. Kemudian untuk menampilkan score, buat hasil.xml
kodingnya kaya gini :
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background2"
android:id="@+id/rel"
>
<TextView android:text="hasil"
android:id="@+id/hasil01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="50px"
android:layout_centerInParent="true">
</TextView>
<Button
android:id="@+id/Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/hasil01"
android:layout_marginBottom="36dp"
android:background="@drawable/btnbackplay" />
<TextView
android:id="@+id/Slamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="108dp"
android:text="YEHET! This is your score^^"
android:textColor="#000000"
android:textSize="30px" />
</RelativeLayout>
3. Selanjutnya kita tinggal masukkan koding di file javanya,
Start.java
package com.hellonumber;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class Start extends Activity implements OnClickListener{
/** Called when the
activity is first created. */
Button btn_jwb, btn_next;
EditText jawabannya;
TextView salahbenar;
ImageView soal;
int benar=0, salah=0, index=0;
int []
pertanyaan={R.drawable.soal1, R.drawable.soal2, R.drawable.soal3,
R.drawable.soal4,
R.drawable.soal5, R.drawable.soal6, R.drawable.soal7,
R.drawable.soal8, R.drawable.soal9, R.drawable.soal10};
String [] jawaban
={"2","5","9","8","2","5","8","6","2","5"};
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.soal);
btn_jwb =
(Button)findViewById(R.id.jawab);
btn_jwb.setOnClickListener(this);
btn_next =
(Button)findViewById(R.id.next);
btn_next.setOnClickListener(this);
salahbenar =
(TextView)findViewById(R.id.salahbenar);
salahbenar.setText("");
soal =
(ImageView)findViewById(R.id.GambarSoal);
soal.setImageResource(pertanyaan[0]);
}
public void onClick(View v)
{
// TODO
Auto-generated method stub
salahbenar=(TextView)findViewById(R.id.salahbenar);
jawabannya=(EditText)findViewById(R.id.jawabannya);
if(v==btn_jwb)
{
String
jawab1=jawabannya.getText().toString();
if(jawab1.equalsIgnoreCase(jawaban[index]))
{
salahbenar.setText("TRUE");benar++;
}
else
{
salahbenar.setText("FALSE");salah++;
}
}
else if (v==btn_next)
{
if(index<pertanyaan.length-1)
{
index++;
soal.setImageResource(pertanyaan[index]);
btn_jwb.setEnabled(true);
btn_next.setVisibility(View.VISIBLE);
jawabannya.setText("");
salahbenar.setText("");
}
else{
finish();
Intent i = new Intent(Start.this,
Hasil.class);
i.putExtra("TRUE", benar);
i.putExtra("FALSE", salah);
startActivity(i);
}
}
}
public void
onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated
method stub
}
}
Hasil .java
package com.hellonumber;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Hasil extends Activity implements OnClickListener{
TextView nilai;
Button back;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hasil);
int benar =
getIntent().getIntExtra("TRUE", 0);
int salah =
getIntent().getIntExtra("FALSE", 0);
nilai
=(TextView)findViewById(R.id.hasil01);
nilai.setText(" True Answer : "+benar+" False Answer
: "+salah);
back=(Button)findViewById(R.id.Back);
back.setOnClickListener(this);
}
public void onClick(View v)
{
// TODO
Auto-generated method stub
if (v==back)
{
finish();
}
}
}
4. Oke, tinggal tambahkan script berikut di AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hellonumber"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Start"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Hasil">
<intent-filter>
<action android:name="android.intent.action.Hasil" />
</intent-filter>
</activity>
</application>
</manifest>
5. Nah, kalo udah di run hasilnya kaya gini :
Ketik jawaban di EditText, kemudian klik Answer, maka akan muncul Tulisan True jika jawaban benar dan tulisan False jika jawaban salah, kemudian klik Next untuk ke soal berikutnya, sampai di Next yang terakhir maka akan muncul total scorenya.
Sekian........... Terima Kasih *_^
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hellonumber"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Start"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Hasil">
<intent-filter>
<action android:name="android.intent.action.Hasil" />
</intent-filter>
</activity>
</application>
</manifest>
5. Nah, kalo udah di run hasilnya kaya gini :
Ketik jawaban di EditText, kemudian klik Answer, maka akan muncul Tulisan True jika jawaban benar dan tulisan False jika jawaban salah, kemudian klik Next untuk ke soal berikutnya, sampai di Next yang terakhir maka akan muncul total scorenya.
Sekian........... Terima Kasih *_^
0 komentar:
Posting Komentar