亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在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條經驗 獲得超13個贊

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

http://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 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號