Android 如何创建包含多个按钮的自定义对话框?

Android 如何创建包含多个按钮的自定义对话框?,android,dialog,Android,Dialog,我想在android中创建自定义对话框,如图所示。 我想打开一个包含多个图像的对话框,并对每个图像执行单击事件操作。 有人能给我指路吗。 我想在单击如图所示的3个点时打开自定义对话框。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:lay

我想在android中创建自定义对话框,如图所示。 我想打开一个包含多个图像的对话框,并对每个图像执行单击事件操作。 有人能给我指路吗。

我想在单击如图所示的3个点时打开自定义对话框。


    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@color/FB_White"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="10dp">

            <TextView
                android:id="@+id/txtTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="Title"
                android:textColor="@color/FB_Black"
                android:textSize="14sp" />

           <!-- <TextView
                android:id="@+id/txtMsg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:padding="5dp"
                android:text="Content"
                android:textColor="@color/FB_White"
                android:textSize="12sp" />-->

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="right">

                <Button
                    android:id="@+id/btnCancel"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_gravity="center"
                    android:background="@drawable/background_btncancel"
                    android:gravity="center"
                    android:text="CANCEL"
                    android:textColor="@color/FB_Orange"
                    android:textSize="12sp" />

                <Button
                    android:id="@+id/btnOk"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="5dp"
                    android:background="@drawable/background_btnok"
                    android:gravity="center"
                    android:text="OK"
                    android:textColor="@color/FB_Orange"
                    android:textSize="12sp" />


            </LinearLayout>
        </LinearLayout>

**Show alertDialog**   


 final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                MainActivity.this);
                        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View view = inflater.inflate(R.layout.custom_alertdialog, null);
                        alertDialogBuilder.setView(view);
                        alertDialogBuilder.setCancelable(true);
                        final AlertDialog dialog = alertDialogBuilder.create();
                        dialog.show();

                        TextView txtTitle = (TextView) dialog.findViewById(R.id.txtTitle);
                        txtTitle.setText("Proceed to Logout?");
                        txtTitle.setTypeface(common.setTypefaceRegular());

                        Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
                        btnCancel.setText("CANCEL");
                        btnCancel.setTypeface(common.setTypefaceSemiBold());
                        btnCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });

                        Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
                        btnOk.setText("OK");
                        btnOk.setTypeface(common.setTypefaceSemiBold());
                        btnOk.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                                deleteLogin();
                                Intent i = new Intent(MainActivity.this, LoginActivity.class);
                                startActivity(i);
                                finish();
                            }
                        });
                        dialog.show();
**显示警报对话框** 最终AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder( 主要活动(本); LayoutFlater充气器=(LayoutFlater)MainActivity.this .getSystemService(上下文布局\充气机\服务); 视图=充气机。充气(R.layout.custom\u alertdialog,null); alertDialogBuilder.setView(视图); alertDialogBuilder.setCancelable(true); 最终AlertDialog=alertDialogBuilder.create(); dialog.show(); TextView txtTitle=(TextView)dialog.findViewById(R.id.txtTitle); setText(“是否继续注销?”); setTypeface(common.setTypefaceRegular()); 按钮btnCancel=(按钮)对话框.findViewById(R.id.btnCancel); btnCancel.setText(“取消”); btnCancel.setTypeface(common.setTypefaceSemiBold()); btnCancel.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ dialog.dismise(); } }); 按钮btnOk=(按钮)dialog.findViewById(R.id.btnOk); btnOk.setText(“确定”); btnOk.setTypeface(common.setTypefaceSemiBold()); btnOk.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ dialog.dismise(); deleteLogin(); 意图i=新意图(MainActivity.this、LoginActivity.class); 星触觉(i); 完成(); } }); dialog.show();
在(…)按钮上单击操作添加以下行

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN, x, y, 0);        
    PopupWindow popup = new PopupWindow(context);
        popup.setContentView(inflater.inflate(R.layout.yourlayout, null, false));
        popup.setWidth(300);
        popup.setHeight(150);
        popup.setFocusable(true);
        popup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    popup.show(event);

如果
R.layout.yourlayout
,请说明需要在弹出窗口中显示的布局。

它是否会如图所示显示@PunithapriyaNo我刚刚添加了示例代码,您可以添加所需的视图。这很简单。您可以使用弹出窗口来显示这种类型的对话框,因为我非常关心您在android studio中使用了哪些LayoutParams.WRAP_内容。
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
View popupView = layoutInflater.inflate(define your layout here, null);  
         final PopupWindow popupWindow = new PopupWindow(
           popupView, 
           LayoutParams.WRAP_CONTENT,  
                 LayoutParams.WRAP_CONTENT);  

         Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
         btnDismiss.setOnClickListener(new Button.OnClickListener(){

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

         popupWindow.showAsDropDown(btnOpenPopup, 50, -30);

}});