AlertDialog或自定义对话框

Van*_*nel 6 android dialog android-2.2-froyo android-alertdialog

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

我想显示一个带有Ok butto的文本,但我想添加我的自定义样式.

使用自定义布局或对话框AlertDialog有什么更好的选择?

Cri*_*ian 29

我会去AlertDialog:

  • 它更容易实现.
  • 您需要做的唯一自定义事项是XML布局,然后对其进行充气.

AlertDialog dialog = new AlertDialog.Builder(this)
    .setView(getLayoutInflater().inflate(R.layout.custom_dialog, null))
    .create();
Run Code Online (Sandbox Code Playgroud)

为了侦听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();
Run Code Online (Sandbox Code Playgroud)

您可以登录android对话框文档:

Dialog类是对话框的基类,但您应该避免直接实例化Dialog.而是使用以下子类之一: