2017-03-01 61 views
0

我創建這個自定義AlertDialog.Builder不能打電話解僱()上的自定義AlertDialog.Builder

的問題是我想關閉它在onClickmPositiveBTN

dismiss()cancel()在沒有定義!怎麼做?

這是它:

public class SendSmsDialog extends AlertDialog.Builder { 

    private TextView mMessageTV; 
    private Button mPositiveBTN; 

    private EditText mobileNumberET; 

    private Context mContext; 

    public SendSmsDialog(@NonNull Context context, String title, String message) { 
     super(context); 
     mContext = context; 

     LayoutInflater inflater = ((MainActivity)context).getLayoutInflater(); 
     View dialoglayout = inflater.inflate(R.layout.send_sms_dialog, null); 
     this.setView(dialoglayout); 

     bindViews(dialoglayout); 
     setListeners(); 


     this.setTitle(title); 
     mMessageTV.setText(message); 
    } 

    private void setListeners() { 
     mPositiveBTN.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mobileNumberET.setEnabled(true); 
       mobileNumberET.requestFocus(); 

       if (UtilitiesMethods.checkEnteredPhone(mobileNumberET.getText().toString())) { 
        ((MainActivity)mContext).doSendSMS(mobileNumberET.getText().toString()); 
       } else { 
        SendSmsDialog.this.dismiss(); //not defined 
        mobileNumberET.setError("Please enter a correct mobile number"); 
       } 
      } 
     }); 
    } 

    private void bindViews(View dialoglayout) { 
     mMessageTV = (TextView) dialoglayout.findViewById(R.id.msg_tv); 
     mPositiveBTN = (Button) dialoglayout.findViewById(R.id.proceed_btn); 
     mobileNumberET = (EditText) dialoglayout.findViewById(R.id.mobile_num); 
    } 
} 

回答

1

我總是用AlertDialog.Builder的是這樣的:

private AlertDialog.Builder builder; 
private AlertDialog dialog; 
. 
. 
. 
. 
    builder = new AlertDialog.Builder(this); 
    builder.setTitle("Add your account"); 
    builder.setView(rootView); 

和用於顯示的對話框我使用:

dialog = builder.show(); 

表演方法返回一個AlertDialog.so我可以駁回:

dialog.dismiss(); 
+0

太棒了,工作!太好了謝謝 :) –

0

建設者本身並不是一個對話框,這意味着你可以不完全呼籲解僱的建設者。

您應該擴展AlertDialog而不是AlertDialog.Builder。

+0

我知道,但我仍然不知道如何終止它! –

相關問題