删除Alertdialog,以便newAlertDialog()将调用onCreateDialog

时间:2014-05-19 17:26:56

标签: android dialog alertdialog

我遇到只调用onCreateDialog一次的问题。这对我来说是一个问题,因为我在该方法中引用了xml布局,每当我关闭对话框并第二次调用它时,这些引用都消失了。代码如下:

        if (position == 0){

        PlayerNameDialog playerNameDialog = PlayerNameDialog.newInstance();
        playerNameDialog.show(getFragmentManager(), TeamActivity.PLAYER_DIALOG);

    }
    else {
        String playerName = teamPlayers.get(position).toString();
        Player selectedPlayer = team.FindPlayer(playerName);
        PlayerNameDialog playerNameDialog = PlayerNameDialog.newInstance();



        playerNameDialog.changePlayerName(selectedPlayer.playerName);
        playerNameDialog.changePlayerRating(selectedPlayer.playerRating);
        playerNameDialog.show(getFragmentManager(), TeamActivity.PLAYER_DIALOG);

    }

和我的提醒对话框

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreateDialog(savedInstanceState);

    LayoutInflater inflater = getActivity().getLayoutInflater();
    view = inflater.inflate(R.layout.player_name_dialog, null);

    playerRatingSeekBar = (SeekBar) view.findViewById(R.id.player_rating_sbar);
    playerRatingSeekBar.setOnSeekBarChangeListener(this);
    playerRatingTxtView = (TextView) view.findViewById(R.id.player_rating);
    playerRatingTxtView.setText(getString(R.string.player_rating)+" "+playerRatingSeekBar.getProgress());
    playerNameEditText = (EditText) view.findViewById(R.id.player_name);

    builder = new AlertDialog.Builder(getActivity());

    builder.setView(view);



    builder.setMessage("New Player");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {

            mListner.onPNDialogPositiveClick(PlayerNameDialog.this, view);

        }

    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            mListner.onPNDialogNegativeClick(PlayerNameDialog.this, view);

        }
    });

    return builder.create();

}

第二次创建新的对话框实例(在else语句下),不再调用onCreateDialog方法。结果所有的edittext引用都消失了。我尝试使用dimiss()和removefragment方法,但没有工作

无论如何要解决这个问题?

提前致谢。

1 个答案:

答案 0 :(得分:1)

try this:
 private static AlertDialog dialog;
public static void showDialog(Context context, String message) {
    if (context != null) {
        TextView textMsg;
        TextView textOk;
        LinearLayout textOKLayout;

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogView = layoutInflator.inflate(R.layout.alert_dialog, null);
        alertDialog.setView(dialogView);
        textMsg = (TextView) dialogView.findViewById(R.id.text);
        textOk = (TextView) dialogView.findViewById(R.id.textOk);
        textOKLayout = (LinearLayout) dialogView.findViewById(R.id.textOKLayout);
        textMsg.setText(message);
        textOKLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });

        dialog = alertDialog.create();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.show();
    } else {
        return;
    }

}