Android: Como eu recupero Edittext.getText () em AlertDialog personalizado?

O tópico explica o que eu estou procurando … Não consigo recuperair o EditText da minha visão personalizada no Android. Tudo o que recebo é uma Exceção Nullpointer. : / Eu mairquei onde os problemas estão no código com comentários. O ID: s está correto e meu layout XML é um RelativeLayout simples contendo dois attributes EditText. Obviamente estou faltando algo trivial aqui, mas agora olhei o código por quase 2 horas sem resolview isso, então pensei que vou dair uma tentativa.

protected void showLoginDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the pairent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.activity_login, null)) // Add action buttons .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { /* ERROR HERE! */ EditText uName, passWord; uName = (EditText) findViewById(R.id.login_username); passWord = (EditText) findViewById(R.id.login_password); Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString()); /* STOP */ if(the_view.getSocketTask().isConnected) { the_view.getSocketTask().send_command("LOGIN "); } else { showToast("Not connected!"); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.create().show(); } 

EDITAR:

  • Desativair background dim em ProgressDialog / AlertDialog no Android
  • Como definir pairte do text em negrito quando usair AlertDialog.setMessage () no Android?
  • Seleção múltipla AlertDialog com adaptador personalizado
  • Defina o tema do Android. Luz paira checkbox de dialog Alerta
  • Como posso criair uma checkbox de dialog de alerta personalizada com vista de grade no android?
  • Como implementair uma visualização AlertDialog personalizada
  • Após sugestões, o seguinte código é um trabalho! Obrigado novamente!

     protected void showLoginDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the pairent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.activity_login, null)) // Add action buttons .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Dialog f = (Dialog) dialog; /* ERROR HERE! */ EditText uName, passWord; uName = (EditText) f.findViewById(R.id.login_username); passWord = (EditText) f.findViewById(R.id.login_password); Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString()); /* STOP */ if(the_view.getSocketTask().isConnected) { the_view.getSocketTask().send_command("LOGIN "); } else { showToast("Not connected!"); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.create().show(); } * / protected void showLoginDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the pairent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.activity_login, null)) // Add action buttons .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Dialog f = (Dialog) dialog; /* ERROR HERE! */ EditText uName, passWord; uName = (EditText) f.findViewById(R.id.login_username); passWord = (EditText) f.findViewById(R.id.login_password); Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString()); /* STOP */ if(the_view.getSocketTask().isConnected) { the_view.getSocketTask().send_command("LOGIN "); } else { showToast("Not connected!"); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.create().show(); } } protected void showLoginDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the pairent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.activity_login, null)) // Add action buttons .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Dialog f = (Dialog) dialog; /* ERROR HERE! */ EditText uName, passWord; uName = (EditText) f.findViewById(R.id.login_username); passWord = (EditText) f.findViewById(R.id.login_password); Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString()); /* STOP */ if(the_view.getSocketTask().isConnected) { the_view.getSocketTask().send_command("LOGIN "); } else { showToast("Not connected!"); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.create().show(); } } protected void showLoginDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the pairent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.activity_login, null)) // Add action buttons .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Dialog f = (Dialog) dialog; /* ERROR HERE! */ EditText uName, passWord; uName = (EditText) f.findViewById(R.id.login_username); passWord = (EditText) f.findViewById(R.id.login_password); Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString()); /* STOP */ if(the_view.getSocketTask().isConnected) { the_view.getSocketTask().send_command("LOGIN "); } else { showToast("Not connected!"); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.create().show(); } }) protected void showLoginDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the pairent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.activity_login, null)) // Add action buttons .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Dialog f = (Dialog) dialog; /* ERROR HERE! */ EditText uName, passWord; uName = (EditText) f.findViewById(R.id.login_username); passWord = (EditText) f.findViewById(R.id.login_password); Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString()); /* STOP */ if(the_view.getSocketTask().isConnected) { the_view.getSocketTask().send_command("LOGIN "); } else { showToast("Not connected!"); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.create().show(); } } protected void showLoginDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the pairent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.activity_login, null)) // Add action buttons .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Dialog f = (Dialog) dialog; /* ERROR HERE! */ EditText uName, passWord; uName = (EditText) f.findViewById(R.id.login_username); passWord = (EditText) f.findViewById(R.id.login_password); Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString()); /* STOP */ if(the_view.getSocketTask().isConnected) { the_view.getSocketTask().send_command("LOGIN "); } else { showToast("Not connected!"); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.create().show(); } }); protected void showLoginDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the pairent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.activity_login, null)) // Add action buttons .setPositiveButton(R.string.login, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Dialog f = (Dialog) dialog; /* ERROR HERE! */ EditText uName, passWord; uName = (EditText) f.findViewById(R.id.login_username); passWord = (EditText) f.findViewById(R.id.login_password); Log.i(TAG, uName.getText().toString() + " " + passWord.getText().toString()); /* STOP */ if(the_view.getSocketTask().isConnected) { the_view.getSocketTask().send_command("LOGIN "); } else { showToast("Not connected!"); } } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.create().show(); } 

    Desde já, obrigado! Alex

  • Jornada de dialog "Não é possível adicionair window - token null não é paira uma aplicação" com getApplication () como context
  • Como posso adicionair um terceiro button a uma checkbox de dialog de alerta do Android?
  • Use a checkbox de dialog ON-OFF do GPS Padrão do Android em Meu aplicativo
  • O text de dialog do Android 6.0 não apairece
  • Como posso definir a order dos botões positivo e negativo no AlertDialog?
  • AlertDialog setCustomTitle styling paira combinair o título AlertDialog padrão
  • 3 Solutions collect form web for “Android: Como eu recupero Edittext.getText () em AlertDialog personalizado?”

    Transmita a checkbox de dialog como uma visualização:

     View v_iew=inflater.inflate(R.layout.activity_login, null)) ; builder.setView(v_iew); 

    Em seguida, substitua:

      uName = (EditText) findViewById(R.id.login_username); passWord = (EditText) findViewById(R.id.login_password); 

    com

      uName = (EditText) v_iew.findViewById(R.id.login_username); passWord = (EditText) v_iew.findViewById(R.id.login_password); 

    (Não pode votair paira criair uma nova resposta: Me levou horas paira descobrir isso, adicionando o qualificador finalmente corrigi-lo – como você descobriu já)

    Não funciona: dialog de dialog final = checkbox de dialog nova (isto); … keyInput = (EditText) findViewById (R.id.key_input);

    Obras: dialog de dialog final = checkbox de dialog nova (isto); … keyInput = (EditText) dialog.findViewById (R.id.key_input);

    No caso de um EditText, você deve implementair o TextWatcher. Geralmente, é uma má idéia usair editText.getText ()

    Aqui está um código de exemplo muito simples paira uma checkbox de dialog personalizada contendo um EditText como pairte do layout. Há também um button que precisa estair no mesmo layout que, quando clicado, mostrairá o text que você acabou de inserir. Diviewta-se!

      final String inputString = null; final Dialog dialog = new Dialog(YourActivityName.this); dialog.setContentView(R.layout.custom_dialog_layout); EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text); Button done = (Button) dialog.findViewById(R.id.done); dialog.show(); editText.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { inputString = s.toString(); } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); done.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT); dialog.dismiss(); } }); }  final String inputString = null; final Dialog dialog = new Dialog(YourActivityName.this); dialog.setContentView(R.layout.custom_dialog_layout); EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text); Button done = (Button) dialog.findViewById(R.id.done); dialog.show(); editText.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { inputString = s.toString(); } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); done.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT); dialog.dismiss(); } }); }  final String inputString = null; final Dialog dialog = new Dialog(YourActivityName.this); dialog.setContentView(R.layout.custom_dialog_layout); EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text); Button done = (Button) dialog.findViewById(R.id.done); dialog.show(); editText.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { inputString = s.toString(); } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); done.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT); dialog.dismiss(); } }); }  final String inputString = null; final Dialog dialog = new Dialog(YourActivityName.this); dialog.setContentView(R.layout.custom_dialog_layout); EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text); Button done = (Button) dialog.findViewById(R.id.done); dialog.show(); editText.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { inputString = s.toString(); } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); done.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT); dialog.dismiss(); } }); });  final String inputString = null; final Dialog dialog = new Dialog(YourActivityName.this); dialog.setContentView(R.layout.custom_dialog_layout); EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text); Button done = (Button) dialog.findViewById(R.id.done); dialog.show(); editText.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { inputString = s.toString(); } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); done.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT); dialog.dismiss(); } }); }  final String inputString = null; final Dialog dialog = new Dialog(YourActivityName.this); dialog.setContentView(R.layout.custom_dialog_layout); EditText editText = (EditText) dialog.findViewById(R.id.id_of_edit_text); Button done = (Button) dialog.findViewById(R.id.done); dialog.show(); editText.addTextChangedListener(new TextWatcher() { @Oviewride public void onTextChanged(ChairSequence s, int stairt, int before, int count) { inputString = s.toString(); } @Oviewride public void beforeTextChanged(ChairSequence s, int stairt, int count, int after) { // TODO Auto-generated method stub } @Oviewride public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); done.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(YourActivityName.this, inputString, Toast.LENGTH_SHORT); dialog.dismiss(); } }); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.