Kamis, 10 Februari 2011

Random Number Genaration Application in Android

GamePage.xml
--------------------------

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#AF7817">
    <TextView android:id="@+id/textLabel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Guess Number Which I Hide Hint between 1 to 100"
    android:lines="2"
    android:textSize="20dip"
    android:textColor="#8AFB17"
    android:layout_marginTop="25dip"   
    />
    <TextView android:id="@+id/textLabel2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter Num:"
    android:textSize="30dip"
    android:textColor="#8AFB17"
    android:layout_marginTop="90dip"   
    />
    <EditText 
    android:id="@+id/tb"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="75dip"
    android:layout_marginLeft="150dip"
    android:layout_marginRight="50dip"
    android:height="20dip"
    android:textSize="30dip"
      />
         <Button
      android:id="@+id/chk"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="***Check***"
      android:layout_marginTop="180dip"
      android:textSize="18dip"
      />
    <TextView 
    android:id="@+id/ans"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="240dip"
        android:layout_marginRight="50dip"
        android:layout_marginLeft="100dip"
    android:height="20dip"
    android:textSize="30dip"
      />
</RelativeLayout>   

GamePage.java
--------------------------
package sra.game;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Game extends Activity implements OnClickListener
{
Button b;
Random r;
int va;
 EditText ed1;
TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamepage);
        r=new Random();
        va=r.nextInt(100);
        this.b=(Button)this.findViewById(R.id.chk);
        this.b.setOnClickListener(this);
    }
    public void onClick(View v){
    
        ed1=(EditText)findViewById(R.id.tb);
    tv=(TextView)findViewById(R.id.ans);
        String value=ed1.getText().toString();
        int val=Integer.parseInt(value);
            if(val==va)
            tv.setText(" Good ");
        else if(val>va)
        {
            tv.setText(" too high ");
            ed1.setText("");
        }
        else if(val<va)
        {
            tv.setText(" too low ");
            ed1.setText("");
        }
    }
}



Tidak ada komentar:

Posting Komentar