android - 删除 Alertdialog,这样 newAlertDialog() 将调用 onCreateDialog

标签 android dialog android-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 方法,但没有用

有办法解决这个问题吗?

提前致谢。

最佳答案

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;
    }

}

关于android - 删除 Alertdialog,这样 newAlertDialog() 将调用 onCreateDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23743714/

相关文章:

android - SensorEvent.timestamp 日期转换问题

html - 如何显示类似于来自 span 元素的对话框消息的弹出窗口

android - 在另一个 tabview 内的 Tabview 内打开 alertDialog

在android中创建警报对话框时出现java.lang.IllegalStateException

android - 使用 SimpleCursorAdapter.ViewBinder 改变 TextView 的颜色

android - 接收事件,如果文件已下载/添加到下载文件夹

android - Android KitKat 上的对话框似乎被剪掉了

Linux 中的 C++ 简单打开文件对话框

android - 我的自定义警报对话框比需要的要高得多

android - 从另一个包开始 Activity