我需要帮助。在我的安卓应用中,我有一个包含一些LinearLayout的ViewFlipper。每个LinearLayout上都有一个ImageView。为了在屏幕之间切换,我使用以下代码:
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
//--------------
public boolean onTouch(View arg0, MotionEvent arg1) {
// Get the action that was done on this touch event
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN: {
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP: {
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX) {
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
vf.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
vf.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
// Flip!
vf.showPrevious();
}
// going forwards: pushing stuff to the left
if (downXValue > currentX) {
// Get a reference to the ViewFlipper
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
// Set the animation
// vf.setInAnimation(AnimationUtils.loadAnimation(this,
// R.anim.push_left_in));
vf.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
vf.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
// Flip!
vf.showNext();
}
break;
}
}
// if you return false, these actions will not be recorded
return true;
}
每个屏幕都包含一个ImageView。当我切换屏幕时,ImageView会改变。现在我想实现这样一个功能:当单击图像时,打开一个URL。以下是我的代码:
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
for (i = 0; i < jArray.length(); i++) {
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
l.setBackgroundColor(0x000000);
l.setOrientation(LinearLayout.VERTICAL);
vf.addView(l);
ImageView img = new ImageView(this);
img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
Drawable image1 = ImageOperations(getBaseContext(), url[i]);
img.setImageDrawable(image1);
System.out.println("target" + target[i]);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://google.com"));
startActivity(browserIntent);
}
});
l.addView(img);
}
问题在于每次我触摸屏幕时,URL都会被打开,现在我无法在不同的屏幕之间切换。如何做到这一点,以区分onClick()和onTouch()? 请帮帮我...提前感谢。