在Android上的onPrepare对话框中自定义AlertDialog内容时的透明背景和无按钮

时间:2011-09-01 19:10:36

标签: android alertdialog showdialog

我在AlertDialog方法上更新onPrepareDialog的内容时遇到了一些困难。

我正在设置AlertDialog的内容,但是对话框出现在没有按钮且没有背景的屏幕上。问题可能与Builder

有关
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_USER_INFORMATION:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        return builder.create();
    default:
        return null;
    }
}

@Override
protected void onPrepareDialog(final int id, final Dialog dialog) {
    switch (id) {
    case DIALOG_USER_INFORMATION:
        createUserInformationAlertDialog(dialog);
        break;
    }
}

public void createUserInformationAlertDialog(Dialog dialogIn) {
    AlertDialog alertDialog = (AlertDialog) dialogIn;
    View dialoglayout = alertDialog.getLayoutInflater().inflate(
            R.layout.dialog_user_info,
            (ViewGroup) findViewById(R.id.dialog_user_layout_root));
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(dialoglayout);
    EventAttendant ea = this.event.getCrowd().getAttendees()
            .get(positionUserToHaveInformationDisplayedOnTheDialog);
    final EventAttendant clone = (EventAttendant) ea.clone();

        // Setting values
        TextView textView = (TextView) dialoglayout.findViewById(R.id.user_name_value);
        textView.setText(ea.getName());

        builder.setPositiveButton(Locale_PT_BR.SEE_ON_FACEBOOK,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {/* User clicked OK so do some stuff */
                    }
                });
        builder.setNegativeButton(Locale_PT_BR.BACK,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {...}
                });
        builder.setView(dialoglayout);
        alertDialog.setView(dialoglayout);
        alertDialog.setContentView(dialoglayout);
}

2 个答案:

答案 0 :(得分:2)

您应该在onCreateDialog()中创建对话框并更改onPrepareDialog()中的文字。

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_USER_INFORMATION:
        return createUserInformationAlertDialog();

    default:
        return null;
    }
}

@Override
protected void onPrepareDialog(final int id, final Dialog dialog) {
    switch (id) {
    case DIALOG_USER_INFORMATION:
        prepareUserInformationAlertDialog((AlertDialog)dialog)
        break;
    }
}

public Dialog createUserInformationAlertDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setPositiveButton(Locale_PT_BR.SEE_ON_FACEBOOK,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {/* User clicked OK so do some stuff */
                }
            });
    builder.setNegativeButton(Locale_PT_BR.BACK,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int whichButton) {...}
            });
    return builder.create();
}

public void prepareUserInformationAlertDialog(AlertDialog alertDialog) {
    EventAttendant ea = this.event.getCrowd().getAttendees()
            .get(positionUserToHaveInformationDisplayedOnTheDialog);
    final EventAttendant clone = (EventAttendant) ea.clone();

    View dialoglayout = alertDialog.getLayoutInflater().inflate(
            R.layout.dialog_user_info, null, false);
    // Setting values               
    TextView textView = (TextView) dialoglayout.findViewById(R.id.user_name_value);
    textView.setText(ea.getName());             
    alertDialog.setView(dialogLayout)
}

我没有测试过此代码,因此可能包含一些错误。

答案 1 :(得分:1)

您只需在dialog.findViewById()方法中调用onPrepareDialog()并更改您的观看内容。

无需再次设置整个布局。

工作示例:

public class CustomDialogActivity extends Activity {

private Button button;
private final int DIALOG_1 = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showDialog(DIALOG_1);
        }
    });
}

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
    case DIALOG_1:
        AlertDialog.Builder builder;

        LayoutInflater inflater = LayoutInflater.from(this);
        View layout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) findViewById(R.id.layout_root));

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText("Hello, this is a custom dialog!");

        builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        return builder.create();
    default:
        dialog = null;
    }
    return dialog;
}

@Override
protected void onPrepareDialog (int id, Dialog dialog) {
    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("I've changed the text from when the dialog was built!");
}
}

XML'object_layout.xml'

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/layout_root"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="10dp"
          >
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:textColor="#FFF"
          />
</LinearLayout>