下面的代码:onTouchListener一个ImageView的需要点击两次工作,另一个只有一次
public class MainActivity extends Activity implements OnTouchListener {
RelativeLayout rl;
int i, j = 0;
final int imageArray[] = { R.drawable.w1, R.drawable.w2, R.drawable.w3 };
int image;
final int imageCount = 3;
ImageView back, save, next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
final int imageArray[] = { R.drawable.w1, R.drawable.w2, R.drawable.w3 };
image = imageArray[0];
rl = (RelativeLayout) findViewById(R.id.rlBackground);
back = (ImageView) findViewById(R.id.bBack);
save = (ImageView) findViewById(R.id.bSave);
next = (ImageView) findViewById(R.id.bNext);
back.setOnTouchListener(this);
save.setOnTouchListener(this);
next.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bBack:
if (j == 0) {
j = imageCount;
}
image = imageArray[j - 1];
rl.setBackgroundResource(image);
j = j - 1;
break;
case R.id.bSave:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeResource(getResources(), image,
opts);
// savePicture(bm, "image_name.jpg");
SaveImage savefile = new SaveImage();
savefile.SaveImagee(this, bm);
Toast.makeText(getApplicationContext(),
"Image saved on your gallery!", Toast.LENGTH_LONG).show();
break;
case R.id.bNext:
if (j != imageCount) {
rl.setBackgroundResource(imageArray[j]);
image = imageArray[j];
j = j + 1;
} else {
j = 0;
rl.setBackgroundResource(imageArray[j]);
image = imageArray[j];
j = j + 1;
}
break;
}
return false;
}
}
的问题:如果我点击保存按钮它适用于第一次点击。如果我点击下一个按钮,我需要点击两次才能触发该功能,但在此之后,如果我继续点击下一个按钮,只需点击一下即可。但是当我切换到按钮后,它需要点击两次,然后只有一次,如果我切换回到下一个按钮,同样的发生 - 两次,一次..我想这与焦点有关。
如果我改变我的ImageView
到ImageButton
,它触发功能的两倍,如果我添加的,如果statemt (event.getAction() == MotionEvent.ACTION_DOWN)
后来我又必须按一下按钮两次。我想按钮与一个工作单击所有的时间。我不明白为什么会这样,因为保存按钮作品只需点击一下,所有的时间..
编辑:如果我改变
image = imageArray[j];
到
image = imageArray[2];
则按钮首先点击工作,但我无法得到它。
你为什么不使用onclicklistener? –
“如果我将ImageView更改为ImageButton,它会触发该函数两次,”如果您更改为onClick()而不是onTouch()',这是真的吗? – codeMagic
您的bNext的其他块始终会导致'j'等于'1'。那真的是你想要的吗? –