0

У меня есть фрагмент диалога с текстом редактирования (пользовательский макет). Я хочу сделать кнопку «Готово» доступной только тогда, когда текст редактирования не пуст. Когда диалоговое окно открывается, это не должно меняться.Android DialogFragment done button clickable

Это мой DialogFragment класс:

public class AddNumberDialog extends DialogFragment { 

private Context mContext; 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    mContext = activity; 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    View mView = inflater.inflate(R.layout.dialog_add_number, null); 

    final EditText mEditText = (EditText) 
      mView.findViewById(R.id.number_edit_text); 

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
    builder.setView(mView) 
      .setPositiveButton(R.string.dialog_done, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        String number = mEditText.getText().toString(); 
        //Add number to list 
       } 
      }) 
      .setNegativeButton(R.string.dialog_cancel, null); 

    return builder.create(); 
} 

} 

ответ

1

Вы должны использовать диалог. Поскольку кнопки диалога оповещения закрывают диалоговое окно оповещений, нажмите. Без выполнения кода в OnClickListener. Вы можете предотвратить отклонение AlertDialog. См. this Answer if someone is interested.

Переопределить метод OnStart, чтобы установить Done Button Clickable False.

public class AddNumberDialog extends DialogFragment { 

     private Context mContext; 

     @Override 
     public void onAttach(Activity activity) { 
      super.onAttach(activity); 
      mContext = activity; 
     } 


    Button button; // Your done Button 


    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     LayoutInflater inflater = LayoutInflater.from(mContext); 
     View mView = inflater.inflate(R.layout.dialog_add_number, null); 

     final EditText mEditText = (EditText) 
       mView.findViewById(R.id.number_edit_text); 

     mEditText.addTextChangedListener(new TextWatcher(){ 
       public void afterTextChanged(Editable s) { 
        if(s.length()>0){ 
          button.setEnabled(true); 
        }else{ 
          button.setEnabled(false); 
        } 
       } 
       public void beforeTextChanged(CharSequence s, int start, int count, int after){} 
       public void onTextChanged(CharSequence s, int start, int before, int count){} 
     }); 


     AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
     builder.setView(mView) 
       .setPositiveButton(R.string.dialog_done, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int id) { 
         // Do Nothing here... We Will be getting the Button Click Listener in Onstart 
        } 
       }) 
       .setNegativeButton(R.string.dialog_cancel, null); 

     return builder.create(); 
    } 

    @Override 
    public void onStart() 
    { 
     super.onStart(); //super.onStart() is where dialog.show() is actually called on the underlying dialog, so we have to do it after this point 

     AlertDialog d = (AlertDialog)getDialog(); 
     if(d != null) 
     {    
      button= (Button) d.getButton(Dialog.BUTTON_NEGATIVE); 
      button.setOnClickListener(new View.OnClickListener() 
        { 
         @Override 
         public void onClick(View v) 
         { 

          String number = mEditText.getText().toString(); 
          //Add number to list 

          Boolean wantToCloseDialog = false; 
          //Do stuff, possibly set wantToCloseDialog to true then... 
          if(wantToCloseDialog) 
           dismiss(); 
          //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false. 
         } 
        }); 
     } 
    } 
} 
+0

Спасибо! Я сделал это в onStart(): – User

+0

Кнопка mButton = ((AlertDialog) getDialog()). GetButton (Dialog.BUTTON_POSITIVE); mButton.setEnabled (false); – User

Смежные вопросы