Berikut adalah kode XML :
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_height="wrap_content" >
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="111dp"
android:layout_y="8dp"
android:text="Pilihan Anda"
android:textAppearance="?android:attr/textAppearanceMedium" />
android:layout_height="wrap_content" >
android:layout_width="123dp"
android:layout_height="wrap_content"
android:layout_x="116dp"
android:layout_y="6dp"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="129dp"
android:layout_y="16dp"
android:text=" " />
android:layout_height="wrap_content" >
android:layout_height="wrap_content" >
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="111dp"
android:layout_y="8dp"
android:text="Pilihan CPU"
android:textAppearance="?android:attr/textAppearanceMedium" />
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="30dp" >
android:layout_width="123dp"
android:layout_height="wrap_content"
android:layout_x="115dp"
android:layout_y="6dp"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
android:layout_height="wrap_content" >
android:layout_width="139dp"
android:layout_height="54dp"
android:layout_x="111dp"
android:layout_y="17dp"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
android:layout_height="wrap_content" >
android:layout_width="wrap_content"
android:layout_height="46dp"
android:layout_x="77dp"
android:layout_y="12dp"
android:text="Copyright 2012 Astandri K." />
//sesuaikan dengan nama package anda
package development.project.rps;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class namaClassAnda extends Activity implements OnClickListener{
/** Called when the activity is first created. */
TextView user, cpu;
TextView status;
Button batu, kertas, gunting;
Button adu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
user = (TextView)findViewById(R.id.user);
cpu = (TextView)findViewById(R.id.cpu);
status = (TextView)findViewById(R.id.status);
batu = (Button)findViewById(R.id.batu);
kertas = (Button)findViewById(R.id.kertas);
gunting = (Button)findViewById(R.id.gunting);
adu = (Button)findViewById(R.id.adu);
batu.setOnClickListener(this);
kertas.setOnClickListener(this);
gunting.setOnClickListener(this);
adu.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.batu :
user.setText("BATU");
break;
case R.id.kertas :
user.setText("KERTAS");
break;
case R.id.gunting :
user.setText("GUNTING");
break;
case R.id.adu :
int comp = (int)(Math.random()*3+1);
if(comp==1)
cpu.setText("BATU");
else if(comp==2)
cpu.setText("KERTAS");
else
cpu.setText("GUNTING");
if(user.getText().equals(cpu.getText())){
status.setText("DRAW");
}
else if(user.getText().equals("BATU") && cpu.getText().equals("KERTAS")){
status.setText("YOU LOSE");
}
else if(user.getText().equals("BATU") && cpu.getText().equals("GUNTING")){
status.setText("YOU WIN");
}
else if(user.getText().equals("KERTAS") && cpu.getText().equals("BATU")){
status.setText("YOU WIN");
}
else if(user.getText().equals("KERTAS") && cpu.getText().equals("GUNTING")){
status.setText("YOU LOSE");
}
else if(user.getText().equals("GUNTING") && cpu.getText().equals("BATU")){
status.setText("YOU LOSE");
}
else if(user.getText().equals("GUNTING") && cpu.getText().equals("KERTAS")){
status.setText("YOU WIN");
}
break;
default :
break;
}
}
}
Tidak ada komentar:
Posting Komentar