3

因此,通过阅读 Google API 指南,我发现了如何在此处的警报对话框中加载自定义布局。

我写了一个扩展 DialogFragment 类的类,如下所示:

    String product;
String description;
String company;
public void getAdInfo(String p, String d, String c)
{
    product = p;
    description = d;
    company = c;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.ad_dialog, null));
            TextView pt = (TextView) getView().findViewById(R.id.product);
            pt.setText(product);
            TextView dt = (TextView) getView().findViewById(R.id.description);
            dt.setText(description);
            TextView ct = (TextView)getView().findViewById(R.id.company);
            ct.setText(company);
           builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // do something
               }
           });
           builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dismiss();
               }
           });
    return builder.create();
}

}

这是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
    android:id="@+id/product"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textIsSelectable="false"
   />
<TextView
    android:id="@+id/company"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textIsSelectable="false"
   />  
<TextView
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textIsSelectable="false"
   />

我用这些行在 OnClickListener 中实例化 Dialog。

AdDialogFragment ad = new AdDialogFragment();
                        ad.getAdInfo(j.getAttribute("product"), j.getAttribute("description"), j.getAttribute("company"));
                        ad.show(getFragmentManager(), null);

j 是来自 xml 节点的元素。

当我单击应该运行对话框的按钮时,我从 LogCat 收到此 NullPointerException 错误:

E/AndroidRuntime(10483):    at com.onix.mallard.android.guifrag.AdDialogFragment.onCreateDialog(AdDialogFragment.java:29)

错误指的是以下行:

pt.setText(product);

最终,它使应用程序完全崩溃。如何动态更改 DialogFragment 的布局?Android API 指南说 DialogFragments 与 Fragments 受相同的生命周期约束,但这并没有告诉我太多,因为它们没有使用 FragmentTransactions(据我所知)。如果这是不可能的并且我需要将信息作为活动实例化,则不会造成任何伤害。

如果有帮助,则从片段中调用该对话框。

4

3 回答 3

4

这是我将如何做到的(未经测试的代码);)

您的自定义对话框:

public class AdDialogFragment extends DialogFragment {
   String mProduct;
   String mDescription;
   String mCompany;
   TextView mProductTV;
   TextView mDescriptionTV;
   TextView mCompanyTV;

   public void setAdInfo(String product, String desc, String company)
   {
       mProduct = product;
       mDescription = desc;
       mCompany = company;
   }
   public AdDialogFragment() {
        super();
   }
   public static AdDialogFragment newInstance() {
        return new AdDialogFragment();
   }

public Dialog onCreateDialog(final Bundle savedInstanceState) {
   LayoutInflater factory = LayoutInflater.from(getActivity());
   final View content = factory.inflate(R.layout.ad_dialog, null);
   mProductTV = (TextView) content.findViewById(R.id.product);
   mDescriptionTV = (TextView) content.findViewById(R.id.description);
   mCompanyTV = (TextView) content.findViewById(R.id.company);

   fillTextViews();

   AlertDialog.Builder dialog;
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      dialog = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.Theme.DeviceDefault.Dialog.)); //can't remember the name or create a custom theme.
   } else {
      dialog = new AlertDialog.Builder(getActivity());//anything below honeycomb can die :).
   }

   //  now customize your dialog
   dialog.setTitle(getActivity().getString(R.string.some_title_if_any))
      .setView(content)
      .setCancelable(true) //this is the default I think.
      .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // do something
               }
           });
      .setNegativeButton("Exit", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dismiss();
               }
           });
   return dialog.create();
}

private void fillTextViews() {
   mProductTV.setText(mProduct);
   mDescriptionTV.setText(mDescription);
   mCompanyTV.setText(mCompany);
}

这就是您从 Activity 调用它的方式,例如:

public void showYourFancyDialog() {
   AdDialogFragment dialog = AdDialogFragment.newInstance();
   dialog.setAdInfo(yourProduct, yourDescription, yourCompany);
   dialog.show(getSupportFragmentManager(), "dialog");
}

使用Google 文档中的模式

于 2013-05-02T18:20:39.393 回答
2

您可以尝试以下方法,它对我有用:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.ad_dialog, null));
builder.setView(v);

然后findViewById像这样调用:

v.findViewById();
于 2013-05-02T17:52:54.347 回答
0

我不久前解决了这个问题,我没有重复使用 AlertDialog 类,而是使用了常规 Activity。在 AndroidManifest.xml 中,活动规范具有以下行。

android:theme="@styles/Theme.HoloLight.Dialog"

一个忠告,线性布局不起作用。尝试相对布局。

于 2013-05-19T01:09:37.773 回答