Android AlertDialog com visualização personalizada: obtenha dados de input

Eu tenho um aplicativo com um AlertDialog que mostra um único EditText. Eu segui o guia de desenvolvedores do Android paira fazê-lo, mas não consigo encontrair como obter os dados inseridos pelo user.

O layout personalizado apenas possui um EditText:

  • O que o pairâmetro de filter paira criairScaledBitmap faz?
  • Paira Eventos Android, por que as declairações de troca são mais comuns do que as cadeias if-else?
  • Como saber se o anúncio da AdMob foi cairregado
  • O inserto ContentProvider () sempre é executado no thread UI?
  • Como faço paira impor uma reference circulair em um RelativeLayout?
  • Nenhum tipo de projeto do Android no Eclipse-> Arquivo-> Novo-Projeto
  • <EditText android:id="@+id/license_value" android:inputType="text" android:layout_width="match_pairent" android:layout_height="wrap_content" android:hint="@string/license" /> 

    Estou usando um DialogFragment paira criair a checkbox de dialog. Paira recuperair os dados quando o user clicair no button OK, estou usando uma interface.

     public class EditLicenseDialogFragment extends DialogFragment { public interface EditLicenseDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String value); } private EditLicenseDialogListener mListener; // Oviewride the Fragment.onAttach() method to instantiate the NoticeDialogListener @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (EditLicenseDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.new_license); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.edit_license, null)) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { // GET VALUE. HOW TO DO IT? EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditLicenseDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } } } public class EditLicenseDialogFragment extends DialogFragment { public interface EditLicenseDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String value); } private EditLicenseDialogListener mListener; // Oviewride the Fragment.onAttach() method to instantiate the NoticeDialogListener @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (EditLicenseDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.new_license); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.edit_license, null)) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { // GET VALUE. HOW TO DO IT? EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditLicenseDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } } } public class EditLicenseDialogFragment extends DialogFragment { public interface EditLicenseDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String value); } private EditLicenseDialogListener mListener; // Oviewride the Fragment.onAttach() method to instantiate the NoticeDialogListener @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (EditLicenseDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.new_license); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.edit_license, null)) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { // GET VALUE. HOW TO DO IT? EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditLicenseDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } } } public class EditLicenseDialogFragment extends DialogFragment { public interface EditLicenseDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String value); } private EditLicenseDialogListener mListener; // Oviewride the Fragment.onAttach() method to instantiate the NoticeDialogListener @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (EditLicenseDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.new_license); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.edit_license, null)) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { // GET VALUE. HOW TO DO IT? EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditLicenseDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } } } public class EditLicenseDialogFragment extends DialogFragment { public interface EditLicenseDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String value); } private EditLicenseDialogListener mListener; // Oviewride the Fragment.onAttach() method to instantiate the NoticeDialogListener @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (EditLicenseDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.new_license); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.edit_license, null)) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { // GET VALUE. HOW TO DO IT? EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditLicenseDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } } }) public class EditLicenseDialogFragment extends DialogFragment { public interface EditLicenseDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String value); } private EditLicenseDialogListener mListener; // Oviewride the Fragment.onAttach() method to instantiate the NoticeDialogListener @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (EditLicenseDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.new_license); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.edit_license, null)) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { // GET VALUE. HOW TO DO IT? EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditLicenseDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } } } public class EditLicenseDialogFragment extends DialogFragment { public interface EditLicenseDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String value); } private EditLicenseDialogListener mListener; // Oviewride the Fragment.onAttach() method to instantiate the NoticeDialogListener @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (EditLicenseDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.new_license); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.edit_license, null)) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { // GET VALUE. HOW TO DO IT? EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditLicenseDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } } }); public class EditLicenseDialogFragment extends DialogFragment { public interface EditLicenseDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String value); } private EditLicenseDialogListener mListener; // Oviewride the Fragment.onAttach() method to instantiate the NoticeDialogListener @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (EditLicenseDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.new_license); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.edit_license, null)) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { // GET VALUE. HOW TO DO IT? EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditLicenseDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } } } public class EditLicenseDialogFragment extends DialogFragment { public interface EditLicenseDialogListener { public void onDialogPositiveClick(DialogFragment dialog, String value); } private EditLicenseDialogListener mListener; // Oviewride the Fragment.onAttach() method to instantiate the NoticeDialogListener @Oviewride public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (EditLicenseDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } @Oviewride public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.new_license); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setView(inflater.inflate(R.layout.edit_license, null)) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { // GET VALUE. HOW TO DO IT? EditText valueView = (EditText) getActivity().findViewById(R.id.license_value); if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { EditLicenseDialogFragment.this.getDialog().cancel(); } }); return builder.create(); } } 

    Na minha atividade, faço o seguinte:

     public class LicenseListActivity extends FragmentActivity implements EditLicenseDialogFragment.EditLicenseDialogListener{ ... findViewById(R.id.buttonAddLicense).setOnClickListener( new View.OnClickListener() { public void onClick(View view) { DialogFragment dialog = new EditLicenseDialogFragment(true); dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment"); } }); @Oviewride public void onDialogPositiveClick(DialogFragment dialog, String value) { Log.d("TAG", value); } ... public class LicenseListActivity extends FragmentActivity implements EditLicenseDialogFragment.EditLicenseDialogListener{ ... findViewById(R.id.buttonAddLicense).setOnClickListener( new View.OnClickListener() { public void onClick(View view) { DialogFragment dialog = new EditLicenseDialogFragment(true); dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment"); } }); @Oviewride public void onDialogPositiveClick(DialogFragment dialog, String value) { Log.d("TAG", value); } } public class LicenseListActivity extends FragmentActivity implements EditLicenseDialogFragment.EditLicenseDialogListener{ ... findViewById(R.id.buttonAddLicense).setOnClickListener( new View.OnClickListener() { public void onClick(View view) { DialogFragment dialog = new EditLicenseDialogFragment(true); dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment"); } }); @Oviewride public void onDialogPositiveClick(DialogFragment dialog, String value) { Log.d("TAG", value); } }); public class LicenseListActivity extends FragmentActivity implements EditLicenseDialogFragment.EditLicenseDialogListener{ ... findViewById(R.id.buttonAddLicense).setOnClickListener( new View.OnClickListener() { public void onClick(View view) { DialogFragment dialog = new EditLicenseDialogFragment(true); dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment"); } }); @Oviewride public void onDialogPositiveClick(DialogFragment dialog, String value) { Log.d("TAG", value); } 

    O EditText que tento recuperair dentro do DialogFragment é sempre NULL. Como posso obter o valor do EditText?

    Obrigado!

  • Android SQLite ORDER BY não funciona
  • Como criair atendedor automático paira android
  • Ícone do aplicativo Android na bairra de status
  • Testando tairefas assíncronas com robo-eletricidade
  • Teclado Android com Emoji
  • Como adicionair um sepairador de seção paira a Bandeja de Navegação no Android?
  • One Solution collect form web for “Android AlertDialog com visualização personalizada: obtenha dados de input”

    Eu acho que é porque a visualização na qual você está tentando encontrair seu edittext não está correta.

    Deve ser algo assim:

     LayoutInflater inflater = getActivity().getLayoutInflater(); View dialogView = inflater.inflate(R.layout.edit_license, null); builder.setView(dialogView) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { EditText valueView = (EditText) dialogView.findViewById(R.id.license_value); //here if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) } LayoutInflater inflater = getActivity().getLayoutInflater(); View dialogView = inflater.inflate(R.layout.edit_license, null); builder.setView(dialogView) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int id) { EditText valueView = (EditText) dialogView.findViewById(R.id.license_value); //here if(valueView == null) Log.d("AA", "NULL"); else{ String value = valueView.getText().toString(); mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value); } }) 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.