Android:alertDialog 不工作

发布于 2024-11-28 20:30:25 字数 2453 浏览 0 评论 0原文

我试图在单击按钮时添加警报对话框,但有些它不起作用......但我添加的吐司工作正常。有人可以帮我吗?我在创建对象时直接添加了上下文而不是“this”[ new AlertDialog.Builder(context).create(); ] 当我添加这个时,它给了我错误“构造函数 AlertDialog.Builder(new View.OnClickListener(){}) 未定义”

       public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        Service service = (Service) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        Drawable d = convertView.findViewById(R.id.submit).getBackground();  
        PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);  
        d.setColorFilter(filter); 
        tv.setText("   " + service.getName());
        this.submitButton = (Button)convertView.findViewById(R.id.submit);
        this.submitButton.setText("Activate");
        this.submitButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                Toast.makeText(context, "Service Activation Request Send", Toast.LENGTH_LONG).show();
                AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                alertDialog.setTitle("Alert 1");
                alertDialog.setMessage("This is an alert");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                    return;
                } }); 
                }
              });

        //this.submitButton.setPadding(20, 0, 0, 0);

        // Depending upon the child type, set the imageTextView01
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        if (service instanceof DataService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.data, 0, 0, 0);
        } else if (service instanceof VoiceService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.voice, 0, 0, 0);
        } else if (service instanceof SmsService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sms, 0, 0, 0);
        }
        return convertView;
        }

I am trying to add an alert dialogue on click of button, but some how it is not working.. but the toast i added is working perfectly. Could somebody please help me out. I've added context directly instead of "this" when creating object [ new AlertDialog.Builder(context).create(); ] as when i was adding this, it was giving me error " The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined"

       public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        Service service = (Service) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        Drawable d = convertView.findViewById(R.id.submit).getBackground();  
        PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);  
        d.setColorFilter(filter); 
        tv.setText("   " + service.getName());
        this.submitButton = (Button)convertView.findViewById(R.id.submit);
        this.submitButton.setText("Activate");
        this.submitButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                Toast.makeText(context, "Service Activation Request Send", Toast.LENGTH_LONG).show();
                AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                alertDialog.setTitle("Alert 1");
                alertDialog.setMessage("This is an alert");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                    return;
                } }); 
                }
              });

        //this.submitButton.setPadding(20, 0, 0, 0);

        // Depending upon the child type, set the imageTextView01
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        if (service instanceof DataService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.data, 0, 0, 0);
        } else if (service instanceof VoiceService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.voice, 0, 0, 0);
        } else if (service instanceof SmsService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sms, 0, 0, 0);
        }
        return convertView;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦言归人 2024-12-05 20:30:25

调用 alertDialog 上的 show() 方法。

Call the show() method on alertDialog.

无戏配角 2024-12-05 20:30:25

您在设置任何字段之前调用 create() 并且没有调用 show()

Android 文档指出,最好在 Activity 的 onCreateDialog(int) 回调方法中定义对话框的创建

http://developer.android.com/guide/topics/ui/dialogs.html

你可以这样做,然后显示你的对话框通过使用相关 ID 调用 showDialog(int) 来实现按钮 onClickListener

You are calling create() before setting any of the fields and also you are not calling show()

The android documentation states that its better to define creation of the dialogs in your our Activity's onCreateDialog(int) callback method

http://developer.android.com/guide/topics/ui/dialogs.html

You could do that and then show your dialog in the button onClickListener by calling showDialog(int) with the relevant Id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文