【问题标题】:How to show Cancel and Accept button in AlertDialog Android如何在 AlertDialog Android 中显示取消和接受按钮
【发布时间】:2019-08-27 21:32:45
【问题描述】:

我在设置警报下部的两个按钮时遇到了麻烦,我想做的是获得与按钮的 IOS 外观相似的外观。 这是我当前的代码:

public void openInstructions() {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    final AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            setNewDateOnView();
            dialog.cancel();
        }
    }).setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    }).create();

    TextView myMsg = new TextView(getActivity());
    myMsg.setText("ExampleText ");
    myMsg.setTextSize(15);
    myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
    dialog.setView(myMsg);

    dialog.setTitle("Confirm Date of Purchase");

    dialog.setOnShowListener( new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface arg0) {
            Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
            negativeButton.setTextColor(0xFFFF0000);
            Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positiveButton.setTextColor(0xFF0000FF);
        }
    });
    dialog.show();

}

现在的外观示例:

我希望它看起来如何:

【问题讨论】:

    标签: java android interface


    【解决方案1】:

    要设计类似 ios 的对话框,我们可以使用这样的自定义布局创建自定义对话框:

    dialog_custom.xml

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:orientation="vertical"
        app:layout_constraintHeight_max="wrap">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/custom_bg">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/title"
                android:gravity="center"
                android:text="Confirm Date of Purchase"
                android:textAlignment="center"
                android:textColor="@android:color/black"
                android:textSize="17sp" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/back_ll"
                android:orientation="vertical"
                android:padding="5dp">
    
                <TextView
                    android:id="@+id/rebooking_single_tv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp"
                    android:textAlignment="center"
                    android:text="@string/dummuy"
                    android:textColor="@android:color/black"
                    android:textSize="17sp" />
            </LinearLayout>
    
            <LinearLayout
                android:id="@+id/back_ll"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_below="@+id/end_time_recycle"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="#8F8F8F" />
                <LinearLayout
                    android:id="@+id/rebooking_back_button_ll"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:orientation="horizontal"
                    android:gravity="center">
    
                    <TextView
                        android:id="@+id/ok_btn"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight=".49"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:text="Ok"
                        android:textAlignment="center"
                        android:textColor="#FF6363"
                        android:textSize="17sp"
                        android:textStyle="bold" />
                    <View
                        android:layout_width="0dp"
                        android:layout_weight=".01"
                        android:layout_height="match_parent"
                        android:layout_below="@+id/end_time_recycle"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:background="#8F8F8F" />
                    <TextView
                        android:id="@+id/cancel_btn"
                        android:layout_width="0dp"
                        android:layout_weight=".5"
                        android:layout_height="wrap_content"
                        android:paddingLeft="10dp"
                        android:textAlignment="center"
                        android:paddingRight="10dp"
                        android:text="Cancle"
                        android:textColor="#7BC5FF"
                        android:textSize="17sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
    

    Main_Activity

    final Dialog dialog=new Dialog(FirstActivity.this);
      dialog.setCancelable(false);
      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
      dialog.setContentView(R.layout.dialog_custom);
      TextView cancel_btn=dialog.findViewById(R.id.cancel_btn);
      cancel_btn.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View view){
            dialog.dismiss();
         }
      });
      TextView ok_btn=dialog.findViewById(R.id.ok_btn);
      ok_btn.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View view){
            dialog.dismiss();
    
         }
      });
      dialog.show();
    

    【讨论】:

      【解决方案2】:

      好吧,作为开始,Google 建议您在创建 Android 应用程序时使用 Android Design Guidelines,实际上您应该坚持使用这些,因为这是您的用户将习惯的(不是 Apple 的)。

      此外,如果您所做的只是显示文本,则没有理由为对话框分配 TextViewAlertDialog 通过 setMessage 提供此功能。

      如果这不能说服您坚持正常的设计规则,唯一的其他选择是创建一个 Custom View 并将其分配给 AlertDialogsetView(类似于您的做法TextView)。

      这是tutorial on creating a Custom View

      【讨论】:

        【解决方案3】:

        您可以创建自定义视图,例如发送的视图并对其进行膨胀

        例子:

                           AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
                           View mView=getActivity().getLayoutInflater().inflate(R.layout.custom_layout, null);
                            mBuilder.setView(mView);
                            Button close = (Button) mView.findViewById(R.id.close);
                            Button OK = (Button) mView.findViewById(R.id.ok);
        
                            close.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                 //close the dialog
                                    hideDialog1();
                                }
                            });
        
                            OK.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                 //do something
                                }
                            });
        
                            dialog = mBuilder.create();
                            dialog.setCanceledOnTouchOutside(false);
                            dialog.show();
        

        【讨论】:

          【解决方案4】:

          使用 .setNeutralButton 使位置最靠左

          TextView 文本视图; 按钮按钮;

          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
          
              button =(Button)findViewById(R.id.button1);
              textview = (TextView)findViewById(R.id.textView1);
          
              button.setOnClickListener(new View.OnClickListener() {
          
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
          
          
                      new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.ic_launcher_background)
          
                              .setTitle("Alert Dialog Box Title")
          
                              .setMessage("Are you sure( Alert Dialog Message )")
          
                              .setPositiveButton("YES", new DialogInterface.OnClickListener()
                              {
                                  @Override
                                  public void onClick(DialogInterface dialog, int which)
                                  {
                                      Toast.makeText(MainActivity.this, "You Clicked on Yes", Toast.LENGTH_SHORT).show();
                                  }
                              })
                              .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                              {
                                  @Override
                                  public void onClick(DialogInterface dialog, int which)
                                  {
                                      Toast.makeText(MainActivity.this, "You Clicked on Cancel", Toast.LENGTH_SHORT).show();
                                  }
                              })
                              .setNeutralButton("NO", new DialogInterface.OnClickListener()
                              {
                                  @Override
                                  public void onClick(DialogInterface dialog, int which)
                                  {
                                      Toast.makeText(MainActivity.this, "You Clicked on NO", Toast.LENGTH_SHORT).show();
                                  }
                              })
                              .show();
                  }
              });
          
          }
          

          ================================================

          【讨论】:

            猜你喜欢
            • 2011-03-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-07-06
            • 2017-01-21
            • 2013-05-11
            • 2017-11-29
            • 1970-01-01
            相关资源
            最近更新 更多