How to display, on MainActivity, the number inserted on a custom alertDialogFragment with EditText?

↘锁芯ラ 提交于 2019-12-02 06:14:57
Janki Gadhiya

You are not setting listener TestAlertDialogListener in your Activity. Just make below changes and you will get it working.

Create a constructor in your TestAlertDialogFragment which will set the listener for us. I am not pasting the whole code of the dialog, only essential fragments. The onCreateDialog method will be as it was.

public class TestAlertDialogFragment extends DialogFragment {

// Use this instance of the interface to deliver action events

TestAlertDialogListener mListener;

public TestAlertDialogFragment(TestAlertDialogListener mListener)
{

    this.mListener=mListener;

}

public interface TestAlertDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog, int i, String string);

    public void onDialogNegativeClick(DialogFragment dialog);
}

Change your activity code like this:

public void showTestAlertDialog(View v) {
// Create an instance of the dialog fragment and show it

DialogFragment d = new TestAlertDialogFragment(MainActivity.this);
d.show(getSupportFragmentManager(), "testAlertDialog");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!