gpt4 book ai didi

java - Intent Activity 不断涌现

转载 作者:行者123 更新时间:2023-12-01 19:32:07 26 4
gpt4 key购买 nike

我为此应用程序设置了一个计时器,因此当计时器结束并且 Itemdata 中的框仍处于选中状态时,它应该调用Thanks.class Activity 。然而,我意识到它对Thanks.class Activity 进行了多次分类。例如。如果选择了 3 个项目,则会调用Thanks.class Activity 三次。

这是我声明的计时器

    //Declare timer
CountDownTimer cTimer = null;

//start timer function
void startTimer() {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent intent = new Intent(ctx, Thanks.class);
ctx.startActivity(intent);
}
};
cTimer.start();

}
//cancel timer
void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}

这是我调用该函数的方式。

@Override
public View getView(final int position, final View view, ViewGroup parent) {
final GridItemData itemData = getItem(position);
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

final View itemView = inflater.inflate(R.layout.grid_image, parent, false);

ivGallery = itemView.findViewById(R.id.grid_item_image);
ivGallery.setImageResource(getItem(position).imageUrl);
final MainActivity main = new MainActivity();

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ivGallery = itemView.findViewById(R.id.grid_item_image);
itemData.setSelected(!itemData.isSelected());
startTimer();
if(itemData == getItem(8)) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
customizeAlert(builder, v);
builder.show();
}
else if (itemData == getItem(0)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.clickfloor: R.drawable.floor);
}
else if (itemData == getItem(1)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.cb: R.drawable.wb);
}
else if (itemData == getItem(2)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.cs: R.drawable.s);
}
else if (itemData == getItem(3)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.clickothers: R.drawable.others);
}
}
});

return itemView;
}

请指教我该怎么做,谢谢!

最佳答案

仅启动一次 CountDownTimer 怎么样?

void startTimer() {
if(cTimer == null) {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent intent = new Intent(ctx, Thanks.class);
ctx.startActivity(intent);
}
};
cTimer.start();
}
}

或者,当第二个 CountDownTimer 即将启动时,停止第一个 CountDownTimer

关于java - Intent Activity 不断涌现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59387153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com