【问题标题】:AlertDialog or Custom DialogAlertDialog 或自定义对话框
【发布时间】:2011-02-07 13:49:33
【问题描述】:

我正在开发一个 Android 2.2 应用程序。

我想显示一个带有确定按钮的文本,但我想添加我的自定义样式。

带有自定义布局的AlertDialogDialog 哪个更好?

【问题讨论】:

    标签: android dialog android-alertdialog android-2.2-froyo


    【解决方案1】:

    我会选择AlertDialog

    • 更容易实现。
    • 您唯一需要做的自定义事情是 XML 布局,然后对其进行扩展。

    AlertDialog dialog = new AlertDialog.Builder(this)
        .setView(getLayoutInflater().inflate(R.layout.custom_dialog, null))
        .create();
    

    为了监听 UI 事件:

    View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
    Button btn = (Button)view.findViewById(R.id.the_id_of_the_button);
    btn.setOnClickListener(blah blah);
    AlertDialog dialog = new AlertDialog.Builder(this)
        .setView(view)
        .create();
    

    您可以签到android dialog docs:

    Dialog 类是对话框的基类,但应避免直接实例化 Dialog。相反,请使用以下子类之一:

    【讨论】:

    • 感谢您的回答。我还有一个问题:如果这个 custom_dialog 布局有一个按钮,我该如何添加一个 OnClickListener?
    【解决方案2】:

    AlertDialog 是 Dialog 的轻量级版本。这应该只处理 INFORMATIVE 问题,这就是与用户的复杂交互受到限制的原因。另一方面,Dialog 可以做更复杂的事情。

    何时使用警报对话框?

    -当我只想告诉用户一些事情时。

    -当我们想要用于提示时,例如您要返回吗(是、否、取消。警报对话框带有 3 个默认提供的按钮,分别是肯定的、否定的和中性的)。

    -当我想提示用户输入一个简单的值(数字/日期/字符串...)时

    什么时候使用对话框?

    -当我想使用更多按钮和小部件进行复杂过程时。

    -示例:

    【讨论】:

      猜你喜欢
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      相关资源
      最近更新 更多