2017-02-13 56 views
1

當按下一個按鈕,我使用一個稍微不同的方法,以保持該對話框打開:AlertDialog setOnShowListener從來沒有所謂

AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this); 
builder.setTitle(R.string.addComponent); 

final EditText titleText = new EditText(NewTableActivity.this); 
titleText.setHint(R.string.title); 

builder.setView(titleText); 
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){ 
    @Override 
    public void onClick(DialogInterface dialogInterface, int i){ 
     dialogInterface.cancel(); 
    } 
}); 
builder.setPositiveButton(R.string.ok, null); 
final AlertDialog alertDialog = builder.create(); 
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){ 
    @Override 
    public void onShow(DialogInterface dialogInterface){ 
     Log.i("TEst", "Doung"); 
     Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
    } 
} 

對話框打開低一點(這個工程太),但日誌從未被稱爲

+0

的可能的複製[爲什麼不昂秀中被調用我的自定義dialogfragment?](http://stackoverflow.com/questions/28522103/why-isnt-onshow-in-my-custom -dialogfragment-being-invoked) –

回答

3
AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this); 
builder.setTitle(R.string.addComponent); 

final EditText titleText = new EditText(NewTableActivity.this); 
titleText.setHint(R.string.title); 

builder.setView(titleText); 
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialogInterface, int i) { 
     dialogInterface.cancel(); 
    } 
}); 
builder.setPositiveButton(R.string.ok, null); 
final AlertDialog alertDialog = builder.create(); 
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { 
    @Override 
    public void onShow(DialogInterface dialogInterface) { 
     Log.e("TEst", "Doung"); 
     Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
    } 
}); 

alertDialog.show(); 
+0

@lucas請替換此文字,因爲我已測試此代碼及其在我的應用程序中運行良好。 –

+0

確保你使用android.support.v4.AlertDialog –

相關問題