我真的不知道为什么这不起作用,我整晚都在尝试。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 和当前代码更新了代码。