是否可以通过按钮确认imageView?(game-charcter)

时间:2016-10-14 10:57:34

标签: android button imageview

我有9个imageview(9个字符)的游戏角色,所以我可以把黄色的戒指放在图片上,如果我选择其中一个很好我在底部的确定按钮我怎么能做这个按钮来确认角色之后它会意图进行另一项活动,但我不知道如何确认我的选择

 imageView = (ImageView)findViewById(R.id.imageView);
    imageView2 = (ImageView)findViewById(R.id.imageView2);
    imageView3 = (ImageView)findViewById(R.id.imageView3);
    imageView4 = (ImageView)findViewById(R.id.imageView4);
    imageView5 = (ImageView)findViewById(R.id.imageView5);
    imageView6 = (ImageView)findViewById(R.id.imageView6);
    imageView7 = (ImageView)findViewById(R.id.imageView7);
    imageView8 = (ImageView)findViewById(R.id.imageView8);
    imageView9 = (ImageView)findViewById(R.id.imageView9);
    button =(Button)findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



        }
    });

}


@Override
public void onClick(View v) {
    Drawable highlight = getResources().getDrawable(R.drawable.highlight);

    switch (v.getId()){
        case R.id.imageView :
            imageView.setBackground(highlight);
            imageView2.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView2 :
            imageView2.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);

            break;

        case R.id.imageView3 :
            imageView3.setBackground(highlight);
            imageView.setBackground(null);
            imageView2.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;


        case R.id.imageView4 :
            imageView4.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView2.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView5 :
            imageView5.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView2.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView6 :
            imageView6.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView2.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView7 :
            imageView7.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView2.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView8 :
            imageView8.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView2.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView9 :
            imageView9.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView2.setBackground(null);
            break;



    }
}

highlight.xml

   <?xml version="1.0" encoding="utf-8"?>
   <shape
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="ring"
      android:innerRadius="50dp"
      android:thickness="10dp"
      android:useLevel="false">

   <solid android:color="#ff0004" />

   </shape>

2 个答案:

答案 0 :(得分:1)

malloc

答案 1 :(得分:0)

我的意思如下:

private int selectedCharacter = 0;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    imageView = (ImageView)findViewById(R.id.imageView);
    imageView2 = (ImageView)findViewById(R.id.imageView2);
    imageView3 = (ImageView)findViewById(R.id.imageView3);
    imageView4 = (ImageView)findViewById(R.id.imageView4);
    imageView5 = (ImageView)findViewById(R.id.imageView5);
    imageView6 = (ImageView)findViewById(R.id.imageView6);
    imageView7 = (ImageView)findViewById(R.id.imageView7);
    imageView8 = (ImageView)findViewById(R.id.imageView8);
    imageView9 = (ImageView)findViewById(R.id.imageView9);
    button =(Button)findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(selectedCharacter == 1) {
                // whatever
            } else if(selectedCharacter == 2) {
                // etc.
            }
        }
    });
}

@Override
public void onClick(View v) {
    Drawable highlight = getResources().getDrawable(R.drawable.highlight);

    switch (v.getId()){
        case R.id.imageView :
            selectedCharacter = 1;
            imageView.setBackground(highlight);
            imageView2.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView2 :
            selectedCharacter = 2;
            imageView2.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView3 :
            selectedCharacter = 3;
            imageView3.setBackground(highlight);
            imageView.setBackground(null);
            imageView2.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;


        case R.id.imageView4 :
            selectedCharacter = 4;
            imageView4.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView2.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView5 :
            selectedCharacter = 5;
            imageView5.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView2.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView6 :
            selectedCharacter = 6;
            imageView6.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView2.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView7 :
            selectedCharacter = 7;
            imageView7.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView2.setBackground(null);
            imageView8.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView8 :
            selectedCharacter = 8;
            imageView8.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView2.setBackground(null);
            imageView9.setBackground(null);
            break;

        case R.id.imageView9 :
            selectedCharacter = 9;
            imageView9.setBackground(highlight);
            imageView.setBackground(null);
            imageView3.setBackground(null);
            imageView4.setBackground(null);
            imageView5.setBackground(null);
            imageView6.setBackground(null);
            imageView7.setBackground(null);
            imageView8.setBackground(null);
            imageView2.setBackground(null);
            break;
    }
}

但@ ik024的答案也很好。