我用setonclicklistener制作了ImageViews,但需要点击两次图片才能工作。我不明白为什么会这样。韦尔普!
我的ImageView的xml,它位于relativelayout:
<ImageView
android:id="@+id/bNext"
android:layout_width="64.785dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:src="@drawable/next" >
</ImageView>
我的ImageView代码:
public class MainActivity extends Activity implements OnClickListener {
...........
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
......
final ImageView next = (ImageView) findViewById(R.id.bNext);
...............
next.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
...................
case R.id.bNext:
if (j != imageCount) {
image = imageArray[j];
rl.setBackgroundResource(image);
j = j + 1;
}
break;
}
}
}
编辑:根据要求,完整代码:
public class MainActivity extends Activity implements OnClickListener {
RelativeLayout rl;
int i, j = 0;
final int imageArray[] = { R.drawable.w1, R.drawable.w2, R.drawable.w3 };
int image;
final int imageCount = 3;
@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);
final ImageView back = (ImageView) findViewById(R.id.bBack);
final ImageView save = (ImageView) findViewById(R.id.bSave);
final ImageView next = (ImageView) findViewById(R.id.bNext);
back.setOnClickListener(this);
save.setOnClickListener(this);
next.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bBack:
if (j != 0) {
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);
SaveImage savefile = new SaveImage();
savefile.SaveImagee(this, bm);
break;
case R.id.bNext:
if (j != imageCount) {
image = imageArray[j];
rl.setBackgroundResource(image);
j = j + 1;
}
break;
}
}
}
答案 0 :(得分:1)
我认为问题在于焦点。您需要按一次View
才能获得焦点,然后再次触发onClick()
。将您的ImageView
更改为ImageButton
并且您不应该遇到此问题,因为Buttons
旨在获得焦点。
只需更改
android:src="@drawable/next"
到
android:background="@drawable/next"
您还可以实现onTouchListener
并在ImageView
上设置,然后听取关键操作以确定要做什么,但我不确定这是否值得。
Here I found an example可能会有所帮助。