Put an EditText in the DialogFragment

0

I need to put an EditText to fill in the name ... that is when the user clicks the button will appear an AlertDialog, which should put the name of the player, I made a step, here in SOpt has a question of this type only unable to resolve.

Could anyone help me?

Code:

package com.gif.popupsair;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

public class MyCaixa extends DialogFragment{

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builderr = new AlertDialog.Builder(getActivity());

        builderr.setMessage("Nome do jogador")
                .setPositiveButton("Salvar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builderr.setTitle("Golll!");
        AlertDialog dialog = builderr.create();

        return dialog;
    }
}

Thank you.

    
asked by anonymous 02.11.2016 / 02:06

2 answers

1

Nathan,

I'll give you a slightly better suggestion. What was done in the answer above @Eduarda can be done without necessarily inheriting from Dialog . AlertDialog has a method called setView() , where you can enter a view customized by you.

We will do this within the onCreateDialog method itself as follows:

View view = LayoutInflater.from(getContext()).inflate(R.layout.seulayoutcustomizado, null);
builderr.setView(view);

Follow the complete code:

package com.gif.popupsair;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

public class MyCaixa extends DialogFragment{

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builderr = new AlertDialog.Builder(getActivity());

        builderr.setMessage("Nome do jogador")
                .setPositiveButton("Salvar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builderr.setTitle("Golll!");

        View view = LayoutInflater.from(getContext()).inflate(R.layout.seulayoutcustomizado, null);
        builderr.setView(view);

        AlertDialog dialog = builderr.create();

        return dialog;
    }
}

It is not advisable to use Dialog out of a DialogFragment , as the response from @Eduarda does. That's because if your Dialog is displayed through a DialogFragment , it will be better connected to the life cycle of Activity . Try debug mode to display a Dialog without a DialogFragment and rotate the screen. You will see in the logs an error like this:

  

Activity x has leaked window com.android .... DecorView

This is because a leak occurred because you did not close the Dialog when the Activity was destroyed during the rotation. What I'm trying to say is that if you display Dialog on your own, you have to tie it to the Activity lifecycle so that no leaks occur. When you use DialogFragment , this type of concern is not required because it is automatic.

    
03.11.2016 / 20:25
1

As a suggestion, you could use a Dialog and set the EditText in XML.

 public class MyCaixa extends Dialog {

    public MyCaixa(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.your_layout);
     }
}

And to call Dialog is enough:

new MyCaixa(context).show();
    
02.11.2016 / 16:55