我遇到了只調用一次onCreateDialog的問題。這對我來說是個問題,因爲我已經在該方法中引用了xml佈局,並且每當我關閉對話框並在第二次調用它時,這些引用都消失了。下面的代碼:刪除Alertdialog,使newAlertDialog()將調用onCreateDialog
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方法,但沒有工作
無論如何解決這個問題?
在此先感謝。
耶創建另一個對話框類應該像你,而不是試圖手動調用'onCreateDialog',你永遠不應該對你有所幫助做 – tyczj