【发布时间】:2015-06-30 06:05:35
【问题描述】:
我有一个自定义的 ActivityIndicator 定义为这个
public class ActivityIndicator extends Dialog
{
private ImageView progress;
private ImageView bottomProgress;
private int type = INDICATOR_SIMPLE;
public static final int INDICATOR_SIMPLE = 0;
public static final int INDICATOR_BOTTOM = 1;
public ActivityIndicator(Context context, int theme, int type)
{
super(context, theme);
this.type = type;
onCreate(null);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_indicator);
progress = (ImageView) findViewById(R.id.progress);
bottomProgress = (ImageView) findViewById(R.id.bottomProgress);
if(type == INDICATOR_BOTTOM)
{
progress.setVisibility(View.INVISIBLE);
}
else if(type == INDICATOR_SIMPLE)
{
bottomProgress.setVisibility(View.INVISIBLE);
}
this.setCancelable(false);
}
@Override
public void show()
{
progress.clearAnimation();
bottomProgress.clearAnimation();
if(type == INDICATOR_BOTTOM)
{
progress.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.rotating_img);
bottomProgress.startAnimation(anim);
}
},400);
}
if(type == INDICATOR_SIMPLE)
{
bottomProgress.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.rotating_img);
progress.startAnimation(anim);
}
},400);
}
super.show();
}
@Override
public void dismiss()
{
super.dismiss();
progress.clearAnimation();
bottomProgress.clearAnimation();
}
}
在我的活动中,我将其初始化为:
indicator = new ActivityIndicator(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen, ActivityIndicator.INDICATOR_SIMPLE);
现在如代码所示,可取消的默认样式为 false。
但是在某些时候我确实想把它取消,这是我的代码:
indicator.setCancelable(true);
indicator.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
finish();
}
});
indicator.show();
当我尝试按下后退按钮时,没有任何反应,dialog 不会取消,也不会取消侦听器。这里有什么问题?为什么按下后退键不自动取消
【问题讨论】:
-
代替
finish()试试dismiss()或dialog.dismiss() -
@Clairvoyant 这就是重点.. 在哪里调用这些 :) 取消监听器不起作用。后按什么都不做
-
抱歉,我没有完整阅读您的问题
-
您不应该破解代码来解决问题。您的问题在于 onCreate。看看我的回答。
标签: java android eclipse android-studio