简体   繁体   English

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

[英]Android: Creating a custom alert dialog inside a dynamic onclicklistener

Not sure how well that title describes what I am hoping to do but here goes. 不确定标题是否能很好地描述我希望做的事情,但是这里有。 Basically I have an app that pragmatically creates a list of buttons which when clicked then return a description. 基本上,我有一个实用的应用程序,可创建按钮列表,单击该按钮后返回说明。

I created the following class 我创建了以下课程

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

 }

This works perfectly as is, however I would very much like to jazz up the dialog box somewhat. 这可以按原样完美运行,但是我非常想让对话框变得有些爵士乐。 I have been looking at some examples online and according to the android docs they suggest defining a custom xml layout and using LayourInflator set the custom xml as the view for the dialog. 我一直在网上查看一些示例,根据android文档,他们建议定义自定义xml布局,并使用LayourInflator将自定义xml设置为对话框视图。 (Well thats how I understood it anyway, probably wrongly right enough) (这就是我无论如何都会理解的,可能是错误地正确)

Although the docs example is slightly different from mine, according to their example I should add the following lines 尽管docs示例与我的示例略有不同,但根据他们的示例,我应该添加以下几行

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

However when I add this to my class, resulting in the following I get an error on getActivity() 但是,当我将其添加到类中时,导致以下结果,我在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();
    }

 }

I did the dialogue box with an EditText to enter email and a button to send the data and to cancel it.. This dialogue box is shown by making the background dim. 我在对话框中输入了EditText,以输入电子邮件,并单击了发送数据和取消数据的按钮。通过使背景变暗来显示此对话框。 And displaying it on top of the current activity. 并将其显示在当前活动的顶部。

    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();
final Context mContext = v.getContext()
inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(inflater.inflate(R.layout.description_dialog, null));

Try this. 尝试这个。

According to CoderDecoder and kleopatra's answers here is the updated version with a shape. 根据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();

Here is the result: 结果如下:

具有形状的动态对话框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM