2015-10-15 70 views
0

在此之前是標記爲重複或垃圾郵件我想你知道,我已經嘗試了以前的問題的所有答案,但毫無效果無法取消AlertDialog

我有一個AlertDialog,我想在失敗的情況下被解僱。因此,這裏是我的代碼:

private AlertDialog openPinDialog() { 
     builder = new AlertDialog.Builder(MainActivity.this); 
     View view = getLayoutInflater().inflate(R.layout.dialog_layout, (ViewGroup)findViewById(R.id.rootLayout)); 
     one = (Button)view.findViewById(R.id.button); 
     two = (Button)view.findViewById(R.id.button2); 
     three = (Button)view.findViewById(R.id.button3); 
     four = (Button)view.findViewById(R.id.button4); 
     five = (Button)view.findViewById(R.id.button5); 
     six = (Button)view.findViewById(R.id.button6); 
     seven = (Button)view.findViewById(R.id.button7); 
     eight = (Button)view.findViewById(R.id.button8); 
     nine = (Button)view.findViewById(R.id.button9); 
     zero = (Button)view.findViewById(R.id.buttonzero); 
     image1 = (ImageView)view.findViewById(R.id.imageView); 
     image2 = (ImageView)view.findViewById(R.id.imageView2); 
     image3 = (ImageView)view.findViewById(R.id.imageView3); 
     image4 = (ImageView)view.findViewById(R.id.imageView4); 
     tv = (TextView)view.findViewById(R.id.changeText); 
     one.setOnClickListener(this); 
     two.setOnClickListener(this); 
     three.setOnClickListener(this); 
     four.setOnClickListener(this); 
     five.setOnClickListener(this); 
     six.setOnClickListener(this); 
     seven.setOnClickListener(this); 
     eight.setOnClickListener(this); 
     nine.setOnClickListener(this); 
     zero.setOnClickListener(this); 
     builder.setView(view); 
     builder.setTitle("Master Password"); 
     return builder.create(); 
    } 

,我想它被辭退:

private void retypePassword(){ 

     checkPinCode.append(String.valueOf(buttonClicked.getText())); 
     if (count == 5){ 
      image1.setImageResource(R.drawable.ic_lens_black_24dp); 
     }else if (count == 6){ 
      image2.setImageResource(R.drawable.ic_lens_black_24dp); 
     }else if (count == 7){ 
      image3.setImageResource(R.drawable.ic_lens_black_24dp); 
     }else if (count == 8){ 
      image4.setImageResource(R.drawable.ic_lens_black_24dp); 
      checkPinCode = checkPinCode.delete(0,4); 
      pinCode = pinCode.delete(4,8); 
      Log.e("1st Try", pinCode.toString()); 
      Log.e("2nd Try", checkPinCode.toString()); 
      if (checkPinCode.toString().equals(pinCode.toString())){ 
       tv.setText(R.string.second_text); 
       Toast.makeText(MainActivity.this, "Pins match", Toast.LENGTH_SHORT).show(); 
      }else { 
       if (openPinDialog() != null && openPinDialog().isShowing()){ 
        openPinDialog().dismiss(); 
       } 
       tv.setText(R.string.first_text); 
       Toast.makeText(MainActivity.this, "Pins don't match", Toast.LENGTH_SHORT).show(); 

       count = 0; 
       image1.setImageResource(R.drawable.ic_radio_button_unchecked_black_24dp); 
       image2.setImageResource(R.drawable.ic_radio_button_unchecked_black_24dp); 
       image3.setImageResource(R.drawable.ic_radio_button_unchecked_black_24dp); 
       image4.setImageResource(R.drawable.ic_radio_button_unchecked_black_24dp); 
       pinCode.delete(0, pinCode.length()); 
       checkPinCode.delete(0, checkPinCode.length()); 

      } 
     } 
    } 

onCreate方法:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     openButton = (Button)findViewById(R.id.button10); 
     openButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       openPinDialog(); 
      } 
     }); 
     pinCode = new StringBuffer(); 
     checkPinCode = new StringBuffer(); 
    } 

結果:

Result

+0

'視圖視圖= LayoutInflater.from(builder.getContext())。膨脹(R.layout.dialog_layout,NULL,假);'使用對話框構建器的上下文進行適當的主題化,對話框還沒有根視圖。 –

回答

0

BEC只要你打電話openPinDialog()它會一直爲警報對話框創建新的對象。

而且您正在檢查新建對話框的不同對象上的所有三個條件。

所以它不會工作,意味着不取消。

撥打電話openPinDialog()只有一次onCreate()或任何其他方法持有類級別成員的引用並使用該成員來檢查您的條件。

喜歡的東西,

Private AlertDialog pinDialog = null; 


onCreate() 
{ 
pinDialog = openPinDialog(); 
} 

而使用像,

if (pinDialog != null && pinDialog.isShowing()){ 
    pinDialog.dismiss(); 
} 
+0

你能給我一個代碼示例嗎? –