如何制作包含多个按钮的自定义对话框?

时间:2017-03-27 12:33:26

标签: android dialog

我想在android中制作自定义对话框,如图所示。 我想打开一个包含多个图像的对话框,并对每个图像上的点击事件执行操作。 任何人都可以指导我。 Custom dialog like this

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在(...)按钮上单击操作添加以下行

def oauth_required(self, method):
    """Decorator that starts the OAuth 2.0 dance.

    Starts the OAuth dance for the logged in user if they haven't already
    granted access for this application.

    Args:
        method: callable, to be decorated method of a webapp.RequestHandler
                instance.
    """

其中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); ,请提及您需要在弹出窗口中显示的布局。

答案 2 :(得分:0)

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

}});