setonclicklistener で ImageView を作成しましたが、動作させるには画像を 2 回クリックする必要があります。なぜこれが起こっているのか理解できません。ああ!
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;
}
}
}