簡體   English   中英

如何實現自己的自定義對話框

[英]How to implement my own custom dialog box

我正在開發一個應用程序,並且想要打開自己的自定義對話框,現在我在使用自己的自定義警報對話框時該如何實現?

enter code here

scene.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final CharSequence[] items = {"auto", "night"};
        AlertDialog.Builder builder = new AlertDialog.Builder(ProActivity.this);
        builder.setTitle("Scene Mode");
        //builder.setTitle("Timer");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {

                if (items[item].equals("auto")) {
                    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ProActivity.this);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putString("preference_scene_mode", "auto");
                    edit.commit();
                } else if (items[item].equals("night")) {
                    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ProActivity.this);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putString("preference_scene_mode", "night");
                    edit.commit();
                }
            }
        });
        builder.show();
    }
});

customdialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/alert_corner">



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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/textView"
            android:text="\u20B9"
            android:textColor="@color/White"
            android:textAlignment="center"
            android:layout_gravity="center"
            android:textSize="@dimen/font_size_normal"
            android:layout_marginLeft="10dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:id="@id/EDTFuelAmount"
            android:background="@drawable/edittext_corner"
            android:padding="10dp"
            android:hint="\u20B9"
            android:textAlignment="center"
            android:layout_marginTop="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="15dp" />
    </LinearLayout>

    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SAVE"
            android:id="@id/BtnFuelAmount"
            android:background="@drawable/button_corner"
            android:textColor="@color/White" />
    </LinearLayout>

要打開自定義對話框時,請在按鈕單擊事件中編寫此代碼

 LayoutInflater li = LayoutInflater.from(context);
 View promptsView = li.inflate(R.layout.customdialog, null);

 final AlertDialog.Builder alertDialogBuilder = new  AlertDialog.Builder(context);
 alertDialogBuilder.setView(promptsView);
 // create alert dialog
 final AlertDialog alertDialog = alertDialogBuilder.create();
 //custom dialog controls                                  
 TextView dialogTextView = (TextView) promptsView.findViewById(R.id.TVMsg);

 Button dialogButton = (Button) promptsView.findViewById(R.id.Btn);

 BtnPick.setOnClickListener(new View.OnClickListener() {
 @Override
     public void onClick(View v) {
        alertDialog.cancel();
     }
 });
 alertDialog.show();

MainActivity.class

     btn1.setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                         DiscountDialog();
                });

    private void DiscountDialog() {
    final Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    dialog.setContentView(R.layout.dialog_discount);
    final EditText etDiscount = (EditText) dialog
            .findViewById(R.id.dialog_discount_editDiscount);

    dialog.findViewById(R.id.dialog_discount_CANCEL).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                }
            });
    dialog.findViewById(R.id.dialog_discount_OK).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
            });
    dialog.show();
    }

mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/subtitle" >

    <LinearLayout
        android:id="@+id/dialog_pos_editnumber_EDITORCONTAINER"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
    android:background="@drawable/layer_list_pos_editnumber_dialogcontainer"
        android:orientation="vertical"
        android:paddingTop="0dp" >



        <com.mobilepaymentspecialists.controls.FEditText
            android:id="@+id/dialog_discount_editDiscount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/layerlist_custom_edittext"
            android:hint="@string/fragment_pos_DISCOUNT_HINT"
            android:inputType="numberDecimal"
            android:digits=".0123456789"
            android:singleLine="true"
            android:minHeight="40dp"
            android:padding="10dp"
            android:textColorHint="@color/subtitle" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2" >    

            <com.mobilepaymentspecialists.controls.FButton
                android:id="@+id/dialog_discount_OK"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="@drawable/selector_green_button"
                android:text="@android:string/ok"
                android:textColor="@color/white"
                android:textStyle="bold" />

            <com.mobilepaymentspecialists.controls.FButton
                android:id="@+id/dialog_discount_CANCEL"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:background="@drawable/selector_green_button"
                android:text="@android:string/cancel"
                android:textColor="@color/white"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

在style.xml中添加以下主題-!>

<style name="Theme_Dialog" parent="android:Theme.Light">
 <item name="android:windowNoTitle">true</item>
 <item name="android:windowBackground">@android:color/transparent</item>

在您的onCreateDialog方法中使用此主題,如下所示:

Dialog dialog = new Dialog(FlightBookActivity.this,R.style.Theme_Dialog);

在xml文件中定義對話框布局(包括標題欄),並按如下所示設置該xml文件:

dialog.setContentView(R.layout.your_dialog_layout);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM