【问题标题】:How to create this type of popup dialogue in Android? [closed]如何在 Android 中创建这种类型的弹出对话框? [关闭]
【发布时间】:2018-01-29 12:46:51
【问题描述】:

这张图片来自 Play 商店的 Deep Relax 应用:

如何创建弹出菜单?我正在创建一个应用,我想请用户提供反馈?

【问题讨论】:

  • 使用警告对话框,设置正面、负面和中性按钮
  • 你能说得更具体些吗?我刚开始android开发。如果可能,请发布一些有用的代码。
  • 您可以花 2 分钟以上的时间找出建议并在 Google 上搜索您刚刚听到的一些新术语。

标签: android menu popup development-environment


【解决方案1】:

为此目的尝试使用自定义对话框:

像这样 custom_dialog_layout 创建布局:

<?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:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="title"
    android:textColor="#000" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting," />


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="description" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@null"
        android:id="@+id/btnNotNOw"
        android:text="not now"
        android:textColor="#040d28" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@null"
        android:text="Never"
        android:id="@+id/btnNever"
        android:textColor="#040d28" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@null"
        android:text="sure"
        android:id="@+id/btnSure"
        android:textColor="#040d28" />

</LinearLayout>

 </LinearLayout>

现在使用以下代码创建对话框:

 final Dialog dialog = new Dialog(LoginActivity.this);
    dialog.setContentView(R.layout.custom_dialog_layout);
    Button btnNotNOw, btnNever, btnSure;
    btnNotNOw = (Button) dialog.findViewById(R.id.btnNotNOw);
    btnNever = (Button) dialog.findViewById(R.id.btnNever);
    btnSure = (Button) dialog.findViewById(R.id.btnSure);
    Window window = dialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.BOTTOM);
    dialog.show();

    btnNever.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // perform your action here 
        }
    });
    btnNotNOw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // perform your action here 
        }
    });
    btnSure.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           // perform your action here 
        }
    });

【讨论】:

    【解决方案2】:

    检查一下

     AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this, AlertDialog.THEME_TRADITIONAL);
    
    
                builder.setMessage("Positive,Negative and Neutral Button")
    
    
                builder.setCancelable(false);
    
    
                builder.setTitle("Custom Dialog");
    
                // Set the positive/yes button click listener
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
    
                    }
                });
    
                // Set the negative/no button click listener
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                    }
                });
    
                // Set the neutral/cancel button click listener
                builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                    }
                });
    
                AlertDialog dialog = builder.create();
                // Display the alert dialog on interface
                dialog.show();
    

    【讨论】:

      【解决方案3】:
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
      
                      // Setting Dialog Title
                      alertDialog.setTitle("Top Heading");
      
                      // Setting Dialog Message
                      alertDialog.setMessage("Message");
      
                      // Setting Positive "Yes" Button
                      alertDialog.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                          // User pressed YES button. Write Logic Here
                          Toast.makeText(getApplicationContext(), "You clicked on Sure",
                                              Toast.LENGTH_SHORT).show();
                          }
                      });
      
                      // Setting Negative "NO" Button
                      alertDialog.setNegativeButton("Not Now", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                          // User pressed No button. Write Logic Here
                          Toast.makeText(getApplicationContext(), "You clicked on Not Now", Toast.LENGTH_SHORT).show();
                          }
                      });
      
                      // Setting Netural "Cancel" Button
                      alertDialog.setNeutralButton("Never", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                          // User pressed Cancel button. Write Logic Here
                          Toast.makeText(getApplicationContext(), "You clicked on Never",
                                              Toast.LENGTH_SHORT).show();
                          }
                      });
      
                      // Showing Alert Message
                      alertDialog.show();
      

      【讨论】:

        【解决方案4】:

        您可以通过自定义对话框创建它:

        自定义对话框:

        为此,您必须创建一个xml 文件并附上。

        final Dialog dialog1 = new Dialog(LabCheckOutActivity.this);
                        dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
                        dialog1.setCancelable(true);
                        dialog1.setContentView(R.layout.dialog_patient_details);
                        dialog1.show();
        

        【讨论】:

          猜你喜欢
          • 2021-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多