简体   繁体   English

getView的DialogFragment在onDismiss上返回null

[英]getView's DialogFragment returning null on onDismiss

I have a custom dialog derived from DialogFragment. 我有一个从DialogFragment派生的自定义对话框。

When the user click in the OK button I need to save the information that is on the screen. 当用户单击确定按钮时,我需要保存屏幕上的信息。

So I made my PositiveButton calls dismiss and I implemented the method onDismiss to save the data. 所以我使我的PositiveButton调用dismiss并且我实现了onDismiss方法来保存数据。

In the onDismiss method I need do get the data from the editView that is on the Dialog. 在onDismiss方法中,我需要从Dialog上的editView获取数据。 I'm using getView().findViewByID to get the editView, but the method GetView() returns null. 我正在使用getView()。findViewByID来获取editView,但方法GetView()返回null。

Here is my code: 这是我的代码:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().dismiss();
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();
}

@override
public void onDismiss(){
    EditView view = (EditView)getView().findViewByID(R.id.edit);
}

I know I can save the view inflated in the OnCreateDialog as a attribute, but that doesn't seems right to me. 我知道我可以将OnCreateDialog中膨胀的视图保存为属性,但这对我来说似乎不对。

How is the right way to get the view from the screen in the onDismiss? 如何在onDismiss中从屏幕上获取视图的正确方法?

Ps: the place where I work don't allow me to post my code, so I took a code from google and I changed it to be as close as possible of my code. Ps:我工作的地方不允许我发布我的代码,所以我从谷歌中获取了代码并将其更改为尽可能接近我的代码。

The way that I did my login DialogFragment was by using a callback method to the fragment's parent activity like such: 我登录DialogFragment是通过对片段的父活动使用回调方法,如下所示:

builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                EditText username = (EditText) getDialog().findViewById(R.id.username);
                EditText password = (EditText) getDialog().findViewById(R.id.password);                 

                un = username.getText().toString();
                pw = password.getText().toString();

                    if (un.equals("") || pw.equals("")) {
                        Toast.makeText(getActivity(), "Username or Password field was empty", Toast.LENGTH_SHORT).show();
                        //Don't login & do something (I made a recursive callback to the fragment that created the dialog)
                    }
                    else if (!un.equals("username") || !pw.equals("password")) {
                        Toast.makeText(getActivity(), "Username or Password was incorrect", Toast.LENGTH_SHORT).show();
                        //Don't login & do something (I made a recursive callback to the fragment that created the dialog)
                    }
                    else if (un.equals("username") && pw.equals("password")) {
                        Toast.makeText(getActivity(), "You have logged in successfully", Toast.LENGTH_SHORT).show();
                        mLogin.Login();
                    }

Then the callback method Login() will create the next fragment that is needed. 然后回调方法Login()将创建所需的下一个片段。

The fragment that will create your login dialog will have the following code within its callback method: 将创建登录对话框的片段在其回调方法中将包含以下代码:

LoginDialog login = new LoginDialog();
login.show(getFragmentManager(), "LOGIN");

Old but gold. 老但金。 This one allows to have bigger control over whole fragment (for example when implementing seekBar or using ButterKnife). 这个允许对整个片段进行更大的控制(例如,在实现seekBar或使用ButterKnife时)。 Enough said: 说够了:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    View view = inflater.inflate(R.layout.dialog_signin, null);
    // do your stuff with views here

    builder.setView(view)
       .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int id) {
               LoginDialogFragment.this.getDialog().dismiss();
           }
       })
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               LoginDialogFragment.this.getDialog().cancel();
           }
       });      
    return builder.create();
}

I know I can save the view inflated in the OnCreateDialog as a attribute, but that doesn't seems right to me. 我知道我可以将OnCreateDialog中膨胀的视图保存为属性,但这对我来说似乎不对。

Yeah, that's the one. 是的,就是那个。 It looks right though, especially when implementing things like seekBar and while using libraries like ButterKnife. 它看起来是正确的,尤其是在实现诸如seekBar之类的东西以及使用像ButterKnife这样的库时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM