为了账号安全,请及时绑定邮箱和手机立即绑定

如何在AlertDialog Android中显示取消和接受按钮

如何在AlertDialog Android中显示取消和接受按钮

ITMISS 2023-06-21 15:32:58
我在设置警报下部的两个按钮时遇到了麻烦,我想做的是获得与 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();}它现在的样子的例子:我想要它的样子:
查看完整描述

4 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

要像 ios 一样设计 Dialog,我们可以使用自定义布局创建自定义对话框,如下所示:


对话框_自定义.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>

主要活动


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

//img1.sycdn.imooc.com//6492a7de0001f8c506481158.jpg

查看完整回答
反对 回复 2023-06-21
?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

好吧,作为开始,谷歌建议您在创建 Android 应用程序时使用Android 设计指南,实际上您应该坚持这些,因为这是您的用户将习惯的(而不是 Apple 的)。

TextView如果您所做的只是显示文本,也没有理由为对话框分配 a , setMessageAlertDialog提供了此功能。

如果这还不能说服您遵守正常的设计规则,唯一的其他选择是创建一个自定义视图并将其分配给AlertDialogwith setView(类似于您使用 的方式TextView)。

查看完整回答
反对 回复 2023-06-21
?
慕的地8271018

TA贡献1796条经验 获得超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();

        }

    });


}

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


查看完整回答
反对 回复 2023-06-21
?
德玛西亚99

TA贡献1770条经验 获得超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();


查看完整回答
反对 回复 2023-06-21
  • 4 回答
  • 0 关注
  • 124 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信