2011-01-25 3 views
0

배경이 이미지 인 버튼에 클릭 가능한 효과를주는 방법은 무엇입니까? 비록 그것이 올바르게 작동하지만 이미지처럼 보이는 것입니다. 어떤 도움을 주시면 감사하겠습니다.ImageButton에 대한 클릭 효과

답변

1

이 내 주사위 게임을했던 것입니다.

은 XML에서 당신은 할 수 있습니다 : 귀하의 MainActivity.Java에서

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

    <ImageButton 
     android:id = "@+id/dice1" 
     android:src = "@drawable/red_1_dice" 
     android:layout_width = "wrap_content" 
     android:layout_height = "wrap_content" 
     android:layout_marginTop = "60px" 
     android:layout_marginLeft = "30px" /> 

    <ImageButton 
     android:id = "@+id/dice2" 
     android:src = "@drawable/red_2_dice" 
     android:layout_width = "wrap_content" 
     android:layout_height = "wrap_content" 
     android:layout_marginLeft = "250px" 
     android:layout_marginTop = "-130px"/> 

    <ImageButton 
     android:id = "@+id/dice3" 
     android:src = "@drawable/red_3_dice" 
     android:layout_width = "wrap_content" 
     android:layout_height = "wrap_content" 
     android:layout_marginLeft = "135px" 
     android:layout_marginTop = "20px" /> 

당신은 할 수 있습니다 :

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ImageButton; 
import android.widget.Button; 
import android.widget.Toast; 
import android.view.View; 
import android.view.View.OnClickListener; 
import java.util.Random; 

public class Z_veselinovic_yahtzeeActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 

    ImageButton button1, button2, button3, button4, button5; 
    Button start, reroll, hold; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Buttons(); 
    } 


    public void Buttons() 
    { 


     button1 = (ImageButton)findViewById(R.id.dice1); 
     button2 = (ImageButton)findViewById(R.id.dice2); 
     button3 = (ImageButton)findViewById(R.id.dice3); 
     button4 = (ImageButton)findViewById(R.id.dice4); 
     button5 = (ImageButton)findViewById(R.id.dice5); 

     start = (Button)findViewById(R.id.Start); 
     reroll = (Button)findViewById(R.id.Reroll); 
     hold = (Button)findViewById(R.id.Hold); 

     reroll.setVisibility(View.GONE); 
     hold.setVisibility(View.GONE); 

     start.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View whatever) 
      { 

       Toast.makeText(getBaseContext(), start.getText() + " Game", Toast.LENGTH_LONG).show(); 

       Random rand1 = new Random(); 
       Random rand2 = new Random(); 
       Random rand3 = new Random(); 
       Random rand4 = new Random(); 
       Random rand5 = new Random(); 

       int dice_num_1 = rand1.nextInt(6) + 1; 
       int dice_num_2 = rand2.nextInt(6) + 1; 
       int dice_num_3 = rand3.nextInt(6) + 1; 
       int dice_num_4 = rand4.nextInt(6) + 1; 
       int dice_num_5 = rand5.nextInt(6) + 1; 



       if(dice_num_1 == 1) 
       { 
        button1.setImageResource(R.drawable.red_1_dice); 
       } 

       else if(dice_num_1 == 2) 
       { 
        button1.setImageResource(R.drawable.red_2_dice); 
       } 

       else if(dice_num_1 == 3) 
       { 
        button1.setImageResource(R.drawable.red_3_dice); 
       } 

       else if(dice_num_1 == 4) 
       { 
        button1.setImageResource(R.drawable.red_4_dice); 
       } 

       else if(dice_num_1 == 5) 
       { 
        button1.setImageResource(R.drawable.red_5_dice); 
       } 

       else if(dice_num_1 == 6) 
       { 
        button1.setImageResource(R.drawable.red_6_dice); 
       } 

도움이 되었기를 바랍니다.