2016-10-14 6 views
0

9画像ビュー(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> 
+0

変数selectedGameCharacterを作成し、各スイッチケースで必要な値に設定します。 –

+0

私はokボタンを押したがって、選択されたキャラクターが私に何を書き込むかわからないことを確認します.clicklistener – CJS

答えて

2
button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if(isSelected(imageView)){ 
      //image view 1 is selected 
     }else if(isSelected(imageView2)){ 
      //image view 2 is selected 
     }else if(isSelected(imageView3)){ 
      //image view 3 is selected 
     } 
     .... // continue for the rest of the imageviews 
    } 
}); 


private boolean isSelected(Imageview iv){ 
    Drawable background = iv.getBackground(); 
    if(background != null) 
     return true; 
    else 
     return false; 
} 
+0

y getbackground? – CJS

+0

あなたの背景に画像の背景を設定する原因:P – ik024

+0

ohh lol sry:D haha​​とthxそしてこれを見てくださいhttp://stackoverflow.com/questions/40038661/how-can-i-change-shape-ring-color-with -a-one-button – CJS

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の回答は素晴らしいです。

関連する問題