将图像添加到按钮

时间:2015-10-03 08:05:53

标签: android arrays loops button

我有一个可以播放数字的幻灯片,现在我希望能够选择使用图片。我的图片是在imageBoard,我不知道如何使它工作。一切都需要在同一个班级。

public void onCreate(Bundle savedInstanceStat) {
    super.onCreate(savedInstanceStat);
    board = new Board();
    board.shuffleBoard();
    updateTileText();
}


public void updateTileText() {
    setContentView(R.layout.activity_game);
    String str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, str11, str12, str13, str14, str15, str;

    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    Button button3 = (Button) findViewById(R.id.button3);
    Button button4 = (Button) findViewById(R.id.button4);
    Button button5 = (Button) findViewById(R.id.button5);
    Button button6 = (Button) findViewById(R.id.button6);
    Button button7 = (Button) findViewById(R.id.button7);
    Button button8 = (Button) findViewById(R.id.button8);
    Button button9 = (Button) findViewById(R.id.button9);
    Button button10 = (Button) findViewById(R.id.button10);
    Button button11 = (Button) findViewById(R.id.button11);
    Button button12 = (Button) findViewById(R.id.button12);
    Button button13 = (Button) findViewById(R.id.button13);
    Button button14 = (Button) findViewById(R.id.button14);
    Button button15 = (Button) findViewById(R.id.button15);
    Button button = (Button) findViewById(R.id.button);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
    button4.setOnClickListener(this);
    button5.setOnClickListener(this);
    button6.setOnClickListener(this);
    button7.setOnClickListener(this);
    button8.setOnClickListener(this);
    button9.setOnClickListener(this);
    button10.setOnClickListener(this);
    button11.setOnClickListener(this);
    button12.setOnClickListener(this);
    button13.setOnClickListener(this);
    button14.setOnClickListener(this);
    button15.setOnClickListener(this);
    button.setOnClickListener(this);


    str1 = String.valueOf(board.getTile(0, 0));
    str2 = String.valueOf(board.getTile(0, 1));
    str3 = String.valueOf(board.getTile(0, 2));
    str4 = String.valueOf(board.getTile(0, 3));
    str5 = String.valueOf(board.getTile(1, 0));
    str6 = String.valueOf(board.getTile(1, 1));
    str7 = String.valueOf(board.getTile(1, 2));
    str8 = String.valueOf(board.getTile(1, 3));
    str9 = String.valueOf(board.getTile(2, 0));
    str10 = String.valueOf(board.getTile(2, 1));
    str11 = String.valueOf(board.getTile(2, 2));
    str12 = String.valueOf(board.getTile(2, 3));
    str13 = String.valueOf(board.getTile(3, 0));
    str14 = String.valueOf(board.getTile(3, 1));
    str15 = String.valueOf(board.getTile(3, 2));
    str = String.valueOf(board.getTile(3, 3));

    button1 = (Button) findViewById(R.id.button1);
    button1.setText(str1);
    button2 = (Button) findViewById(R.id.button2);
    button2.setText(str2);
    button3 = (Button) findViewById(R.id.button3);
    button3.setText(str3);
    button4 = (Button) findViewById(R.id.button4);
    button4.setText(str4);
    button5 = (Button) findViewById(R.id.button5);
    button5.setText(str5);
    button6 = (Button) findViewById(R.id.button6);
    button6.setText(str6);
    button7 = (Button) findViewById(R.id.button7);
    button7.setText(str7);
    button8 = (Button) findViewById(R.id.button8);
    button8.setText(str8);
    button9 = (Button) findViewById(R.id.button9);
    button9.setText(str9);
    button10 = (Button) findViewById(R.id.button10);
    button10.setText(str10);
    button11 = (Button) findViewById(R.id.button11);
    button11.setText(str11);
    button12 = (Button) findViewById(R.id.button12);
    button12.setText(str12);
    button13 = (Button) findViewById(R.id.button13);
    button13.setText(str13);
    button14 = (Button) findViewById(R.id.button14);
    button14.setText(str14);
    button15 = (Button) findViewById(R.id.button15);
    button15.setText(str15);
    button = (Button) findViewById(R.id.button);
    button.setText(str);

}

public void imageBoard(){
    int[] duck = {
            R.drawable.duck_0,
            R.drawable.duck_1,
            R.drawable.duck_2,
            R.drawable.duck_3,
            R.drawable.duck_4,
            R.drawable.duck_5,
            R.drawable.duck_6,
            R.drawable.duck_7,
            R.drawable.duck_8,
            R.drawable.duck_9,
            R.drawable.duck_10,
            R.drawable.duck_11,
            R.drawable.duck_12,
            R.drawable.duck_13,
            R.drawable.duck_14,
            R.drawable.duck_15

    };

}

@Override
public void onClick(View v) {

    counter++;
    switch (v.getId()) {

        case R.id.button1:

            board.moveTile(0, 0);
            updateTileText();
            break;
        case R.id.button2:
            board.moveTile(0, 1);
            updateTileText();
            break;
        case R.id.button3:
            board.moveTile(0, 2);
            updateTileText();
            break;
        case R.id.button4:
            board.moveTile(0, 3);
            updateTileText();
            break;
        case R.id.button5:
            board.moveTile(1, 0);
            updateTileText();
            break;
        case R.id.button6:
            board.moveTile(1, 1);
            updateTileText();
            break;
        case R.id.button7:
            board.moveTile(1, 2);
            updateTileText();
            break;
        case R.id.button8:
            board.moveTile(1, 3);
            updateTileText();
            break;
        case R.id.button9:
            board.moveTile(2, 0);
            updateTileText();
            break;
        case R.id.button10:
            board.moveTile(2, 1);
            updateTileText();
            break;
        case R.id.button11:
            board.moveTile(2, 2);
            updateTileText();
            break;
        case R.id.button12:
            board.moveTile(2, 3);
            updateTileText();
            break;
        case R.id.button13:
            board.moveTile(3, 0);
            updateTileText();
            break;
        case R.id.button14:
            board.moveTile(3, 1);
            updateTileText();
            break;
        case R.id.button15:
            board.moveTile(3, 2);
            updateTileText();
            break;
        case R.id.button:
            board.moveTile(3, 3);
            updateTileText();

            break;
    }

3 个答案:

答案 0 :(得分:0)

只需使用Button代替{{1}}。

答案 1 :(得分:0)

    public void onCreate(Bundle savedInstanceStat) {
    super.onCreate(savedInstanceStat);
    board = new Board();
    board.shuffleBoard();
    updateTileText();
}


public void updateTileText() {
    setContentView(R.layout.activity_game);
    String str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, str11, str12, str13, str14, str15, str;

    ImageButton button1 = (ImageButton) findViewById(R.id.button1);
    ImageButton button2 = (ImageButton) findViewById(R.id.button2);
    ImageButton button3 = (ImageButton) findViewById(R.id.button3);
    ImageButton button4 = (ImageButton) findViewById(R.id.button4);
    ImageButton button5 = (ImageButton) findViewById(R.id.button5);
    ImageButton button6 = (ImageButton) findViewById(R.id.button6);
    ImageButton button7 = (ImageButton) findViewById(R.id.button7);
    ImageButton button8 = (ImageButton) findViewById(R.id.button8);
    ImageButton button9 = (ImageButton) findViewById(R.id.button9);
    ImageButton button10 = (ImageButton) findViewById(R.id.button10);
    ImageButton button11 = (ImageButton) findViewById(R.id.button11);
    ImageButton button12 = (ImageButton) findViewById(R.id.button12);
    ImageButton button13 = (ImageButton) findViewById(R.id.button13);
    ImageButton button14 = (ImageButton) findViewById(R.id.button14);
    ImageButton button15 = (ImageButton) findViewById(R.id.button15);
    ImageButton button = (ImageButton) findViewById(R.id.button);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
    button4.setOnClickListener(this);
    button5.setOnClickListener(this);
    button6.setOnClickListener(this);
    button7.setOnClickListener(this);
    button8.setOnClickListener(this);
    button9.setOnClickListener(this);
    button10.setOnClickListener(this);
    button11.setOnClickListener(this);
    button12.setOnClickListener(this);
    button13.setOnClickListener(this);
    button14.setOnClickListener(this);
    button15.setOnClickListener(this);
    button.setOnClickListener(this);


    str1 = String.valueOf(board.getTile(0, 0));
    str2 = String.valueOf(board.getTile(0, 1));
    str3 = String.valueOf(board.getTile(0, 2));
    str4 = String.valueOf(board.getTile(0, 3));
    str5 = String.valueOf(board.getTile(1, 0));
    str6 = String.valueOf(board.getTile(1, 1));
    str7 = String.valueOf(board.getTile(1, 2));
    str8 = String.valueOf(board.getTile(1, 3));
    str9 = String.valueOf(board.getTile(2, 0));
    str10 = String.valueOf(board.getTile(2, 1));
    str11 = String.valueOf(board.getTile(2, 2));
    str12 = String.valueOf(board.getTile(2, 3));
    str13 = String.valueOf(board.getTile(3, 0));
    str14 = String.valueOf(board.getTile(3, 1));
    str15 = String.valueOf(board.getTile(3, 2));
    str = String.valueOf(board.getTile(3, 3));

    button1 = (ImageButton) findViewById(R.id.button1);
    button1.setText(str1);
    button2 = (ImageButton) findViewById(R.id.button2);
    button2.setText(str2);
    button3 = (ImageButton) findViewById(R.id.button3);
    button3.setText(str3);
    button4 = (ImageButton) findViewById(R.id.button4);
    button4.setText(str4);
    button5 = (ImageButton) findViewById(R.id.button5);
    button5.setText(str5);
    button6 = (ImageButton) findViewById(R.id.button6);
    button6.setText(str6);
    button7 = (ImageButton) findViewById(R.id.button7);
    button7.setText(str7);
    button8 = (ImageButton) findViewById(R.id.button8);
    button8.setText(str8);
    button9 = (ImageButton) findViewById(R.id.button9);
    button9.setText(str9);
    button10 = (ImageButton) findViewById(R.id.button10);
    button10.setText(str10);
    button11 = (ImageButton) findViewById(R.id.button11);
    button11.setText(str11);
    button12 = (ImageButton) findViewById(R.id.button12);
    button12.setText(str12);
    button13 = (ImageButton) findViewById(R.id.button13);
    button13.setText(str13);
    button14 = (ImageButton) findViewById(R.id.button14);
    button14.setText(str14);
    button15 = (ImageButton) findViewById(R.id.button15);
    button15.setText(str15);
    button = (ImageButton) findViewById(R.id.button);
    button.setText(str);

}

public void imageBoard(){
    int[] duck = {
            R.drawable.duck_0,
            R.drawable.duck_1,
            R.drawable.duck_2,
            R.drawable.duck_3,
            R.drawable.duck_4,
            R.drawable.duck_5,
            R.drawable.duck_6,
            R.drawable.duck_7,
            R.drawable.duck_8,
            R.drawable.duck_9,
            R.drawable.duck_10,
            R.drawable.duck_11,
            R.drawable.duck_12,
            R.drawable.duck_13,
            R.drawable.duck_14,
            R.drawable.duck_15

    };

}

@Override
public void onClick(View v) {

    counter++;
    switch (v.getId()) {

        case R.id.button1:

            board.moveTile(0, 0);
            updateTileText();
            break;
        case R.id.button2:
            board.moveTile(0, 1);
            updateTileText();
            break;
        case R.id.button3:
            board.moveTile(0, 2);
            updateTileText();
            break;
        case R.id.button4:
            board.moveTile(0, 3);
            updateTileText();
            break;
        case R.id.button5:
            board.moveTile(1, 0);
            updateTileText();
            break;
        case R.id.button6:
            board.moveTile(1, 1);
            updateTileText();
            break;
        case R.id.button7:
            board.moveTile(1, 2);
            updateTileText();
            break;
        case R.id.button8:
            board.moveTile(1, 3);
            updateTileText();
            break;
        case R.id.button9:
            board.moveTile(2, 0);
            updateTileText();
            break;
        case R.id.button10:
            board.moveTile(2, 1);
            updateTileText();
            break;
        case R.id.button11:
            board.moveTile(2, 2);
            updateTileText();
            break;
        case R.id.button12:
            board.moveTile(2, 3);
            updateTileText();
            break;
        case R.id.button13:
            board.moveTile(3, 0);
            updateTileText();
            break;
        case R.id.button14:
            board.moveTile(3, 1);
            updateTileText();
            break;
        case R.id.button15:
            board.moveTile(3, 2);
            updateTileText();
            break;
        case R.id.button:
            board.moveTile(3, 3);
            updateTileText();

            break;
    }

在layout.xml中

   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/button1"
      android:src="@drawable/abc"/>

答案 2 :(得分:0)

在按钮上使用setBackgroundResource()并传递按钮上的drawable id,为其指定背景。