我有以下活动。在这项活动中,我有超过10张图片。图像随后退和后退按钮而变化。我想保存此活动的状态。例如,如果我在第5张图片,我关闭了活动或应用程序。现在,当我启动活动或应用程序时,它从第一张图片开始,而不是从第五张图片开始。它应该从我离开它的第5张图片开始。请帮我编写代码或编辑我的代码。
int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 /// so on};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
iButton = (Button) findViewById(R.id.bNext);
gButton = (Button) findViewById(R.id.bPrev);
//Just set one Click listener for the image
iButton.setOnClickListener(iButtonChangeImageListener);
gButton.setOnClickListener(gButtonChangeImageListener);
}
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage++;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};
View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage--;
currentImage = (currentImage + images.length) % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};
请建议使用代码。
答案 0 :(得分:0)
尝试使用SharedPreferences存储您的上一个图像位置。
private ImageView hImageViewPic;
private Button iButton;
private Button gButton;
private SharedPreferences sharedPreferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hImageViewPic = (ImageView) findViewById(R.id.idImageViewPic);
iButton = (Button) findViewById(R.id.bNext);
gButton = (Button) findViewById(R.id.bPrev);
sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
hImageViewPic.setImageResource(images[sharedPreferences.getInt("LastSeenImagePosition", 0)]);
//Just set one Click listener for the image
iButton.setOnClickListener(iButtonChangeImageListener);
gButton.setOnClickListener(gButtonChangeImageListener);
}
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage++;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("LastSeenImagePosition", currentImage);
editor.commit();
}
};
View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage--;
currentImage = (currentImage + images.length) % images.length;
hImageViewPic.setImageResource(images[currentImage]);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("LastSeenImagePosition", currentImage);
editor.commit();
}
};
答案 1 :(得分:0)
int currentImage;
int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 /// so on};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences settings = this.getSharedPreferences("imageNumber", 0);
if(settings != null){
currentImage= settings .getInt("lastImage", 0);
}
hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
iButton = (Button) findViewById(R.id.bNext);
gButton = (Button) findViewById(R.id.bPrev);
//Just set one Click listener for the image
iButton.setOnClickListener(iButtonChangeImageListener);
gButton.setOnClickListener(gButtonChangeImageListener);
}
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage++;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};
View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage--;
currentImage = (currentImage + images.length) % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};
在onPause方法:
@Override
protected void onPause(){
super.onPause();
SharedPreferences settings = getSharedPreferences("imageNumber", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("lastImage",currentImage );
editor.commit();
}
答案 2 :(得分:0)
上述建议可能有效但执行方式是使用活动' s - onSaveInstanceState和onRestoreInstanceState。您可以查看以下示例: Saving Android Activity state using Save Instance State
或者阅读android手册:http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
主要优点是只有在真正需要时才会保存这些值。