我真的不知道爲什麼這不起作用,我一直試圖通宵。ImageView
應該減去分數,當我點擊它。我也嘗試過記錄它,但沒有成功..可能是因爲我的動畫干擾了某種方式?爲什麼我的OnClick for ImageView無法正常工作?
這是我整個XML
<TextView
android:id="@+id/timer1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test" />
<TextView
android:id="@+id/score1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Score:" />
<ImageView
android:id="@+id/imgview1"
android:layout_width="83dp"
android:layout_height="131dp"
android:clickable="true"
android:onClick="PeanutClick"
android:src="@drawable/lil_peanut"/>
這是我的源代碼
public class GameView extends Activity implements OnClickListener {
MediaPlayer backgroundMusic;
TextView mTextField;
TextView mScoreField;
Point size = new Point();
Display display;
ImageView image;
int score = 0;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameviewlayout);
int l;
mTextField = (TextView) findViewById(R.id.timer1);
mScoreField = (TextView) findViewById(R.id.score1);
image = (ImageView) findViewById(R.id.imgview1);
image.setOnClickListener(this);
TranslateAnimation moveLefttoRight = new TranslateAnimation(0, 200, 0,
0);
moveLefttoRight.setDuration(5000);
moveLefttoRight.setFillAfter(true);
moveLefttoRight.setRepeatCount(-1);
moveLefttoRight.setRepeatMode(Animation.REVERSE);
image.startAnimation(moveLefttoRight);
new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("Seconds Remaining: " + millisUntilFinished
/1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
display = getWindowManager().getDefaultDisplay();
display.getSize(size);
int width = size.x;
int height = size.y;
backgroundMusic = MediaPlayer
.create(GameView.this, R.raw.gallery_music);
backgroundMusic.start();
}
public void onClick(View v) {
if (v.getId() == R.id.imgview1)
Log.i("MB2", "Touchy touchy!");
PeanutClick();
}
public void PeanutClick() {
score = score - 100;
mScoreField.setText("Score: " + score);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
backgroundMusic.release();
}
編輯;發佈的解決方案不起作用。我用我的整個XML和當前代碼更新了代碼。
您在清單中定位了哪個API版本? – Grambot 2013-04-23 21:44:05
您的代碼是否正在執行PeanutClick?如果你在第一行放置一個斷點(score = -100),它會被擊中嗎? – RvdK 2013-04-23 21:50:44
gameviewLayout.xml是什麼樣的佈局?這是偶然的框架佈局嗎?有一些佈局的情況下,你的對象將被遮蓋,而不是其領域的最前面的東西。有些人通過將佈局中的對象(在這種情況下爲您的ImageVew)進一步向下移動來解決問題,因此它會高於其他衝突項目。 – HalR 2013-04-24 00:39:25