处理 main activity 中 dialogfragment 中的按钮

Handling the buttons from dialogfragment in main activity

我想在主 activity 中处理新游戏按钮和添加玩家按钮。如您所见,我在我的 warningdialog class 中定义了视图(它扩展了 DialogFragment,我尝试使用 getter 并尝试调用它但是我得到空指针异常,我也尝试了其他方法但是应用程序崩溃。

最好的方法是什么?我想这样做的原因是因为我在 main activity 中有其他方法我想在按下按钮时调用,所以我认为从 main activity 处理对话框按钮是最好的选择.

这是我的 Warningdialog,它从 dialogfragment

扩展而来
public class WarningDialog extends DialogFragment {

public Button mNewGame, mAddPlayersBtn;
private ImageButton mCloseBtn;


/**
 * Method to close the dialog
 */
private View.OnClickListener mBtnListener = new View.OnClickListener(){
    @Override
        public void onClick(View v){
            dismiss();
    }
};

public WarningDialog() {

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogTheme);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.newgame_dialog,container,false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mAddPlayersBtn = (Button) view.findViewById(R.id.btnAddPlayers);
    mNewGame = (Button) view.findViewById(R.id.btnNewGame);
    mCloseBtn = (ImageButton) view.findViewById(R.id.btnClose);

    //closing the dialog
    mCloseBtn.setOnClickListener(mBtnListener);

}


public Button getmNewGame() {
    return mNewGame;
}

主要activity方法:

    private void warningDialog(){
    final WarningDialog warningDialog = new WarningDialog();
    warningDialog.show(getSupportFragmentManager(),"Warning");

    warningDialog.getmNewGame().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
        }
    });

}

您可以从 DialogFragment 中调用 Activity.onActivityResult,然后根据请求和响应代码做出适当的响应。我认为您的方法行不通,因为按钮需要在 onCreateView 等中初始化,您的 activity 不会知道这些。

public class WarningDialog extends DialogFragment {
    public static int RC_WARNING = 100;
    public static int RC_NEW_GAME = 101;
    public static int RC_ADD_PLAYER = 102;

    /**
     * Method to handle clicks in the dialog
     */
    private View.OnClickListener mBtnListener = new View.OnClickListener(){
        @Override
        public void onClick(View v){
            if (v == mCloseBtn) {
              dismiss();
            } else if (v == mNewGame) {
              getActivity().onActivityResult(RC_WARNING, RC_NEW_GAME, null);
            } else if (v == mAddPlayersBtn) {
              getActivity().onActivityResult(RC_WARNING, RC_ADD_PLAYER, null);
            }
        }
    };
}

// In your activity:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (request_code == WarningDialog.RC_WARNING) {
        if (resultCode == WarningDialog.RC_NEW_GAME) {
          // new game logic
        } else if (resultCode == WarningDialog.RC_ADD_PLAYER) {
          // add player logic
        }        
    }
}

您还可以使用 onActivityResult 上的 Intent 参数传递其他数据。

您需要做的就是让 DialogFragment 回调到 Activity 中的 public 方法。

在 DialogFragment 中,为新游戏按钮添加一个新的 OnClickListener:

/**
 * Method to start new game
 */
private View.OnClickListener mNewGameBtnListener = new View.OnClickListener(){
    @Override
        public void onClick(View v){
            ((MainActivity)getActivity()).doNewGame();
            dismiss();
    }
};

然后将其设置为新游戏按钮的点击侦听器:

 mNewGame = (Button) view.findViewById(R.id.btnNewGame);
 mNewGame.setOnClickListener(mNewGameBtnListener);

然后,在 Activity 中定义单击 DialogFragment 按钮时将调用的 public 方法:

public void doNewGame() {
    //User chose new game!
}