【问题标题】:Is there any way to get Android Dialog box like the one in Google Apps?有没有办法获得像 Google Apps 中那样的 Android 对话框?
【发布时间】:2017-10-04 09:17:58
【问题描述】:

这是我需要的截图:


(来源:quoracdn.net

我想在我的应用程序中添加一个这样的拨号框,而不是默认的丑陋的 android 拨号框。

()

是否有任何 Google 库(如 Material ?)允许我在不将整个内容重新编码为片段的情况下执行此操作?

谢谢

【问题讨论】:

  • 创建自定义对话框
  • 有许多库可以根据您的需要制作相同的对话框。这些对话被称为材料对话,我确信有一个支持库。如果您不想浏览代码,那么只需在您的 style.xml 文件中创建一个样式并将该样式传递给您的对话框。
  • @Umair 我会看看库,谢谢伙计

标签: android dialog fragment


【解决方案1】:

使用底部对话框。

MainActivity.java

import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    LinearLayout backgroundLayout;
    View bottomSheetView;
    TextView textPrompt1, textPrompt2;
    TextView textSDK;
    BottomSheetDialog bottomSheetDialog;
    BottomSheetBehavior bottomSheetBehavior;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textPrompt1 = (TextView)findViewById(R.id.prompt1);
        textPrompt2 = (TextView)findViewById(R.id.prompt2);
        backgroundLayout = (LinearLayout)findViewById(R.id.backgroundlayout);

        bottomSheetView = getLayoutInflater().inflate(R.layout.bottomsheetdialog_layout, null);
        bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
        bottomSheetDialog.setContentView(bottomSheetView);
        bottomSheetBehavior = BottomSheetBehavior.from((View) bottomSheetView.getParent());
        bottomSheetBehavior.setBottomSheetCallback(bottomSheetCallback);

        bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                textPrompt1.setText("OnShow");
            }
        });

        bottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                textPrompt1.setText("OnDismiss");
            }
        });

        backgroundLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                bottomSheetDialog.show();
            }
        });

    }

    BottomSheetBehavior.BottomSheetCallback bottomSheetCallback =
            new BottomSheetBehavior.BottomSheetCallback(){
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {
                    switch (newState){
                        case BottomSheetBehavior.STATE_COLLAPSED:
                            textPrompt2.setText("COLLAPSED");
                            break;
                        case BottomSheetBehavior.STATE_DRAGGING:
                            textPrompt2.setText("DRAGGING");
                            break;
                        case BottomSheetBehavior.STATE_EXPANDED:
                            textPrompt2.setText("EXPANDED");
                            break;
                        case BottomSheetBehavior.STATE_HIDDEN:
                            textPrompt2.setText("HIDDEN");
                            bottomSheetDialog.dismiss();
                            break;
                        case BottomSheetBehavior.STATE_SETTLING:
                            textPrompt2.setText("SETTLING");
                            break;
                        default:
                            textPrompt2.setText("unknown...");
                    }
                }

                @Override
                public void onSlide(@NonNull View bottomSheet, float slideOffset) {

                }
            };
}

创建文件layout/bottomsheetdialog_layout.xml,定义BottomSheetDialog的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Bottom Sheet Dialog Example"
            android:textSize="26dp"
            android:textStyle="bold"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>

    </LinearLayout>
</LinearLayout>

输出:

【讨论】:

  • 不错!但是我必须自己设计它,我想我会先去 GitHub 库看看我是否找到了一些看起来像的东西。还是谢谢
  • 当然,您也可以尝试其他库。如果您可以根据要求设置样式,您可以更改 bottomsheetdialog_layout.xml 文件并从通用实用程序类调用对话框。
【解决方案2】:

你可以DialogFragment。

public class CustomDialog extends DialogFragment {

    //region Variables

    //options will be true if there is 2 option and false if there is a single option
    private boolean options;
    private WeakReference<DialogListener> mDialogListener;

    //endregion

    //region Constructors

    public CustomDialog() {
    }

    //endregion

    //region Instances

    public static CustomDialog newInstance(WeakReference<DialogListener> listener) {
        CustomDialog fragment = new CustomDialog();
        fragment.mDialogListener = listener;
        return fragment;
    }

    //endregion

    //region Lifecycle methods

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        Bundle bundle = getArguments();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view =inflater.inflate(R.layout.custom_alert_layout,null);
        builder.setView(view);

        TextView title = view.findViewById(R.id.alert_dialog);
        TextView positive = view.findViewById(R.id.positive);
        TextView negative = view.findViewById(R.id.negative);

        positive.setText("Ok");
        negative.setText("Cancel"));
        title.setText("title");


        positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mDialogListener!=null) {
                    mDialogListener.get().onDialogPositiveClick(CustomDialog.this);
                }
                dismiss();
            }
        });
        negative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mDialogListener!=null) {
                    mDialogListener.get().onDialogNegativeClick(CustomDialog.this);
                }
                dismiss();
            }
        });
        // Create the AlertDialog object and return it
        return builder.create();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);

    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    //endregion

    //region Interface

    /**
     * Interface to tranasfer the click to the Dialogs host
     * The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     */
    public interface DialogListener {
        void onDialogPositiveClick(DialogFragment dialog);
        void onDialogNegativeClick(DialogFragment dialog);
    }

    //endregion
}

你想在哪里展示它

DialogFragment c = CustomDialog.newInstance(new WeakReference<CustomDialog.DialogListener>(this));
c.setArguments(bundle);
c.show(getChildFragmentManager(), "dialogFrag");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2020-03-24
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多