Android:在动态onclicklistener中创建自定义警报对话框

时间:2013-03-26 11:33:00

标签: android android-alertdialog

不确定这个标题对我希望做什么有多好,但是这里有。基本上我有一个应用程序,实用地创建一个按钮列表,当单击时,然后返回描述。

我创建了以下类

public class DynamicOnClickListener implements OnClickListener
{

    String description;
    public DynamicOnClickListener(String adesc) {
        //sets the description attribute at instantiation
         this.description = adesc;
    }

    public void onClick(View v) {
        //on button click returns dialog box with description in it
        Log.v("DynamicOnClickListener","1");
        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
        builder.setMessage(description);
        builder.setCancelable(false);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) 
                   {
                       dialog.cancel();
                   }
               });
        AlertDialog alert = builder.create();   
        alert.show();
    }

 }

这样可以完美地工作,但我非常想在某种程度上对对话框进行爵士乐。我一直在网上看一些例子,根据android文档,他们建议定义一个自定义的xml布局,并使用LayourInflator将自定义xml设置为对话框的视图。 (那就是我怎么理解它,可能是错误的正确的)

虽然文档示例与我的略有不同,但根据他们的示例,我应该添加以下行

// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))

但是当我将它添加到我的班级时,导致以下内容我在getActivity()上出现错误

public class DynamicOnClickListener implements OnClickListener
{

    String description;
    public DynamicOnClickListener(String adesc) {
        //sets the description attribute at instantiation
         this.description = adesc;
    }

    public void onClick(View v) {
        //on button click returns dialog box with program description in it
        Log.v("DynamicOnClickListener","1");
        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.description_dialog, null));


        builder.setMessage(description);
        builder.setCancelable(false);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) 
                   {
                       dialog.cancel();
                   }
               });
        AlertDialog alert = builder.create();   
        alert.show();
    }

 }

3 个答案:

答案 0 :(得分:3)

我在EditText的对话框中输入了电子邮件和一个发送数据并取消它的按钮。 通过使背景变暗来显示此对话框。并将其显示在当前活动之上。

    final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
               LinearLayout popUp = new LinearLayout(this);
               popUp.setBackgroundColor(Color.LTGRAY);
               popUp.setOrientation(1);

                EditText t1 = new  EditText(this);
                t1.setHint("Enter Your Email ID ");
                t1.setTextColor(Color.parseColor("#FFFFFF"));
                t1.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);          

                LinearLayout btnLayout = new LinearLayout(this);
                btnLayout.setBackgroundColor(Color.LTGRAY);
                btnLayout.setOrientation(0);
                btnLayout.setGravity(Gravity.CENTER);

                Button send = new  Button(this);
                send.setText("Send");
                send.setTextColor(Color.WHITE);
                Button cancel = new  Button(this);
                cancel.setText("Cancel");
                cancel.setTextColor(Color.WHITE);

                btnLayout.addView(send);
                btnLayout.addView(cancel);
                popUp.addView(t1);
                popUp.addView(btnLayout);

                dialog.setContentView(popUp);
              cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
                dialog.show();

答案 1 :(得分:2)

final Context mContext = v.getContext()
inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(inflater.inflate(R.layout.description_dialog, null));

试试这个。

答案 2 :(得分:0)

根据CoderDecoder和kleopatra的回答,这里是带有形状的更新版本。

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    GradientDrawable shape = new GradientDrawable();
    shape.setColor(getResources().getColor(R.color.shape_client_textview_background_color));
    shape.setStroke(10, getResources().getColor(R.color.shape_client_textview_border_color));
    shape.setCornerRadius(50);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    LinearLayout popUp = new LinearLayout(this);
   // popUp.setBackgroundColor(Color.LTGRAY);
    layoutParams.setMargins(20,20,20,20);
    popUp.setOrientation(LinearLayout.VERTICAL);
    popUp.setLayoutParams(layoutParams);
    popUp.setBackground(shape);
    popUp.setPadding(20,20,20,20);

    EditText t1 = new EditText(this);
    t1.setHint("Enter Your Email ID ");
    t1.setTextColor(Color.parseColor("#FFFFFF"));
    t1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

    LinearLayout btnLayout = new LinearLayout(this);
 //   btnLayout.setBackgroundColor(Color.LTGRAY);
    btnLayout.setOrientation(LinearLayout.HORIZONTAL);
    btnLayout.setGravity(Gravity.CENTER);

    Button send = new Button(this);
    send.setText("Send");
    send.setTextColor(Color.WHITE);
    Button cancel = new Button(this);
    cancel.setText("Cancel");
    cancel.setTextColor(Color.WHITE);

    btnLayout.addView(send);
    btnLayout.addView(cancel);
    popUp.addView(t1);
    popUp.addView(btnLayout);

    dialog.setContentView(popUp);
    cancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });

    dialog.show();

结果如下:

Dynamic dialog with a shape