目标:能够点击按钮,使图片发生变化并保持在该图像上。问题是我不能更改 imagebutton9 上的图片。当我点击 imagebutton9 时,它会更改 imagebutton1 上的图片。所以问题是有人知道是什么导致了这种情况的发生吗?
包 com.example.tictactoe;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class TicTacMain extends Activity implements View.OnClickListener {
/*****************************************************
* Makes imagebuttons and views.
*****************************************************/
private ImageButton imageButton1;
private ImageView imageView1;
private ImageButton imageButton2;
private ImageView imageView2;
private ImageButton imageButton3;
private ImageView imageView3;
private ImageButton imageButton4;
private ImageView imageView4;
private ImageButton imageButton5;
private ImageView imageView5;
private ImageButton imageButton6;
private ImageView imageView6;
private ImageButton imageButton7;
private ImageView imageView7;
private ImageButton imageButton8;
private ImageView imageView8;
private ImageButton imageButton9;
private ImageView imageView9;
/*****************************************************
* Makes imagebuttons and views.
*****************************************************/
/*** alternate between X and O ***/
private int changer = 0;
/*** alternate between X and O ***/
/*****************************************************
* Stops further change of pictures after first change
*****************************************************/
private int antiChange1 = 0;
private int antiChange2 = 0;
private int antiChange3 = 0;
private int antiChange4 = 0;
private int antiChange5 = 0;
private int antiChange6 = 0;
private int antiChange7 = 0;
private int antiChange8 = 0;
private int antiChange9 = 0;
/*****************************************************
* Stops further change of pictures after first change
*****************************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_main);
Toast.makeText(this, "Inni onCreate", Toast.LENGTH_LONG).show();
Toast.makeText(this, "Øverst onClick", Toast.LENGTH_LONG).show();
this.imageButton1 = (ImageButton) this.findViewById(R.id.imageButton1);
this.imageButton1.setOnClickListener((OnClickListener) this);
this.imageButton2 = (ImageButton) this.findViewById(R.id.imageButton2);
this.imageButton2.setOnClickListener((OnClickListener) this);
this.imageButton3 = (ImageButton) this.findViewById(R.id.imageButton3);
this.imageButton3.setOnClickListener((OnClickListener) this);
this.imageButton4 = (ImageButton) this.findViewById(R.id.imageButton4);
this.imageButton4.setOnClickListener((OnClickListener) this);
this.imageButton5 = (ImageButton) this.findViewById(R.id.imageButton5);
this.imageButton5.setOnClickListener((OnClickListener) this);
this.imageButton6 = (ImageButton) this.findViewById(R.id.imageButton6);
this.imageButton6.setOnClickListener((OnClickListener) this);
this.imageButton7 = (ImageButton) this.findViewById(R.id.imageButton7);
this.imageButton7.setOnClickListener((OnClickListener) this);
this.imageButton8 = (ImageButton) this.findViewById(R.id.imageButton8);
this.imageButton8.setOnClickListener((OnClickListener) this);
this.imageButton9 = (ImageButton) this.findViewById(R.id.imageButton9);
this.imageButton9.setOnClickListener((OnClickListener) this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tic_tac_main, menu);
return true;
}
public int tellerbytter() {
Toast.makeText(this, "Tellerbytter", Toast.LENGTH_LONG).show();
if (changer == 0) {
return changer = 1;
} else if (changer == 1) {
return changer = 0;
}
return -1;
}
// ///imageButton1.setOnClickListener(new OnClickListener());
// @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton1:
if (v.getId() == R.id.imageButton1) {
if (changer == 1 && antiChange1 == 0) {
imageView1 = (ImageView) findViewById(R.id.imageButton1);
imageView1.setImageResource(R.drawable.o);
tellerbytter();
antiChange1++;
break;
} else if (changer == 0 && antiChange1 == 0) {
imageView1 = (ImageView) findViewById(R.id.imageButton1);
imageView1.setImageResource(R.drawable.x1);
tellerbytter();
antiChange1++;
break;
}
}
case R.id.imageButton2:
if (v.getId() == R.id.imageButton2) {
if (changer == 1 && antiChange2 == 0) {
imageView2 = (ImageView) findViewById(R.id.imageButton2);
imageView2.setImageResource(R.drawable.o);
tellerbytter();
antiChange2++;
break;
} else if (changer == 0 && antiChange2 == 0) {
imageView2 = (ImageView) findViewById(R.id.imageButton2);
imageView2.setImageResource(R.drawable.x1);
tellerbytter();
antiChange2++;
break;
}
}
case R.id.imageButton3:
if (v.getId() == R.id.imageButton3) {
if (changer == 1 && antiChange3 == 0) {
imageView3 = (ImageView) findViewById(R.id.imageButton3);
imageView3.setImageResource(R.drawable.o);
tellerbytter();
antiChange3++;
break;
} else if (changer == 0 && antiChange3 == 0) {
imageView3 = (ImageView) findViewById(R.id.imageButton3);
imageView3.setImageResource(R.drawable.x1);
tellerbytter();
antiChange3++;
break;
}
}
case R.id.imageButton4:
if (v.getId() == R.id.imageButton4) {
if (changer == 1 && antiChange4 == 0) {
imageView4 = (ImageView) findViewById(R.id.imageButton4);
imageView4.setImageResource(R.drawable.o);
tellerbytter();
antiChange4++;
break;
} else if (changer == 0 && antiChange4 == 0) {
imageView4 = (ImageView) findViewById(R.id.imageButton4);
imageView4.setImageResource(R.drawable.x1);
tellerbytter();
antiChange4++;
break;
}
}
case R.id.imageButton5:
if (v.getId() == R.id.imageButton5) {
if (changer == 1 && antiChange5 == 0) {
imageView5 = (ImageView) findViewById(R.id.imageButton5);
imageView5.setImageResource(R.drawable.o);
tellerbytter();
antiChange5++;
break;
} else if (changer == 0 && antiChange5 == 0) {
imageView5 = (ImageView) findViewById(R.id.imageButton5);
imageView5.setImageResource(R.drawable.x1);
tellerbytter();
antiChange5++;
break;
}
}
case R.id.imageButton6:
if (v.getId() == R.id.imageButton6) {
if (changer == 1 && antiChange6 == 0) {
imageView6 = (ImageView) findViewById(R.id.imageButton6);
imageView6.setImageResource(R.drawable.o);
tellerbytter();
antiChange6++;
break;
} else if (changer == 0 && antiChange6 == 0) {
imageView6 = (ImageView) findViewById(R.id.imageButton6);
imageView6.setImageResource(R.drawable.x1);
tellerbytter();
antiChange6++;
break;
}
}
case R.id.imageButton7:
if (v.getId() == R.id.imageButton7) {
if (changer == 1 && antiChange7 == 0) {
imageView7 = (ImageView) findViewById(R.id.imageButton7);
imageView7.setImageResource(R.drawable.o);
tellerbytter();
antiChange7++;
break;
} else if (changer == 0 && antiChange7 == 0) {
imageView7 = (ImageView) findViewById(R.id.imageButton7);
imageView7.setImageResource(R.drawable.x1);
tellerbytter();
antiChange7++;
break;
}
}
case R.id.imageButton8:
if (v.getId() == R.id.imageButton8) {
if (changer == 1 && antiChange8 == 0) {
imageView8 = (ImageView) findViewById(R.id.imageButton8);
imageView8.setImageResource(R.drawable.o);
tellerbytter();
antiChange8++;
break;
} else if (changer == 0 && antiChange8 == 0) {
imageView8 = (ImageView) findViewById(R.id.imageButton8);
imageView8.setImageResource(R.drawable.x1);
tellerbytter();
antiChange8++;
break;
}
}
case R.id.imageButton9:
Toast.makeText(this, "utenfor knapp9", Toast.LENGTH_LONG).show();
if (v.getId() == R.id.imageButton9) {
Toast.makeText(this, "knapp9", Toast.LENGTH_LONG).show();
if (changer == 1 && antiChange9 == 0) {
imageView9 = (ImageView) findViewById(R.id.imageButton9);
imageView9.setImageResource(R.drawable.o);
tellerbytter();
antiChange9++;
break;
} else if (changer == 0 && antiChange9 == 0) {
imageView1 = (ImageView) findViewById(R.id.imageButton1);
imageView1.setImageResource(R.drawable.x1);
tellerbytter();
antiChange9++;
break;
}
}
}
}
然后是项目的 XML。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TicTacMain" >
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/linearLayout1" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="89dp" >
<ImageButton
android:id="@+id/imageButton7"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton8"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton9"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout2"
android:layout_below="@+id/linearLayout3"
android:layout_marginTop="51dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton6"
android:layout_width="80dip"
android:layout_height="80dip"
android:src="@drawable/ic_launcher" />
</LinearLayout>