0

我正在尝试创建一个警报对话框,其中列出了所有当前安装的启动器,当按下一个时,该启动器将运行。

我已经设法使代码正常工作,但是每当我返回我的活动时,屏幕上仍会显示对话框。如何在其子元素的 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();
}
4

3 回答 3

1

您可以在查看单击时关闭 AlertDialog:

d.setOnClickListener(new OnClickListener(){
   @Override
    public void onClick(View arg0) 
     {
           //....your code here.....
           if(null !=alrt){
              if(alrt.isShowing())
                alrt.dismiss()
           }      
     }

  });
于 2013-06-08T05:22:44.173 回答
1

好的,实现它的一种方法是使 AlertDialog 成为活动范围的变量,而不是在 onClick 事件中声明它。我不太喜欢那样做,因为它在活动之外没有用,但除非有人能想到更好的东西,否则我将不得不这样做

private AlertDialog gAlrt;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    ...
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
            ...
}

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();
                gAlrt.cancel();

                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);

            }

        });

        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);
    gAlrt = dlgAlert.create();


    gAlrt.show();
}
于 2013-06-08T06:37:47.173 回答
0

在孩子的点击事件中

onClick(...){
    dialogname.dismiss();
}
于 2013-06-08T05:23:07.553 回答