0
如何爲背景爲圖片的按鈕提供可點擊效果?雖然它功能正常,但它看起來像一幅圖像。任何幫助,將不勝感激。ImageButtons的可點擊效果
如何爲背景爲圖片的按鈕提供可點擊效果?雖然它功能正常,但它看起來像一幅圖像。任何幫助,將不勝感激。ImageButtons的可點擊效果
您必須創建不同的圖像並將它們放入選擇器中。看看本教程爲進一步的細節:
這是我做我的骰子游戲。
在你的XML,你可以這樣做:
在你MainActivity.Java你可以這樣做:
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);
}
我希望這有助於。
感謝Christian!它爲我工作! – Pa1 2011-02-07 03:09:22