我正在尝试创建一个警报对话框,其中列出了所有当前安装的启动器,当按下一个时,该启动器将运行。
我已经设法使代码正常工作,但是每当我返回我的活动时,屏幕上仍会显示对话框。如何在其子元素的 onclick 侦听器中关闭/隐藏/取消警报对话框?
我无法将对话框传递到 onClickListener 中,因为在我声明它时它尚未创建
public void btnLaunchers_Click(View v)
{
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
l.setOrientation(LinearLayout.VERTICAL);
LayoutInflater inflater = this.getLayoutInflater();
for (ResolveInfo r : gLstLaunchers)
{
View d = inflater.inflate(R.layout.adapter_psudo_launcheritem, null);
ImageView i = (ImageView) d.findViewById(R.id.Adapter_LauncherItem_Image);
TextView t = (TextView) d.findViewById(R.id.Adapter_LauncherItem_Text);
LinkedList<Object> tTag = new LinkedList<Object>();
tTag.add(r);
d.setTag(tTag);
d.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0)
{
LinkedList<Object> o = (LinkedList<Object>) arg0.getTag();
ResolveInfo r = (ResolveInfo) o.get(0);
ActivityInfo activity=r.activityInfo;
ComponentName name=new ComponentName(activity.applicationInfo.packageName,
activity.name);
Intent i=new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivity(i);
//Toast.makeText(arg0.getContext(), intent, Toast.LENGTH_LONG).show();
}
});
i.setImageDrawable(r.loadIcon(this.getPackageManager()));
t.setText(r.loadLabel(this.getPackageManager()));
l.addView(d);
}
dlgAlert.setView(l);
dlgAlert.setPositiveButton("Cancel", null);
dlgAlert.setCancelable(true);
AlertDialog alrt = dlgAlert.create();
alrt.show();
}