我們已經創建了兩個自定義對話框,一個是關於另一個是警告。當我在拖拽自定義對話框中交替選擇時,該按鈕不起作用。爲什麼自定義對話框按鈕在android中不工作
示例代碼
AlertDialog.Builder builder;
Context mContext;
LayoutInflater inflater;
View layout;
Dialog dialog;
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case 1:
builder = null;
mContext = this;
inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.alert_page, (ViewGroup) findViewById(R.id.alert_Root));
Button alertUser = (Button) layout.findViewById(R.id.alert_Submit);
alertUser.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
dialog.dismiss();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
dialog.show();
break;
case 2:
builder = null;
mContext = this;
inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.about_page, (ViewGroup) findViewById(R.id.about_Root));
Button aboutUser = (Button) layout.findViewById(R.id.about_Submit);
aboutUser.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Log.e("About","About");
try
{
Log.e("About1","About");
dialog.dismiss();
}
catch (Exception e)
{
Log.e("About","About12");
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
dialog.show();
break;
}
return dialog;
}
例如我正在使用兩個按鈕。第一個按鈕稱爲case 1
,第二個按鈕稱爲case 2
。
我被選中第一個按鈕訪問case 1
,然後選擇自定義對話框alertUser
按鈕successfully Exit the dialog box
。
立即選中第二個按鈕訪問case 2
,然後選擇自定義對話框aboutUser
按鈕successfully Exit the dialog box
。
之後立即選中第一個按鈕訪問case 1
,然後選擇自定義對話框alertUser
按鈕Now the dialog box does not exist (button is now not working)
。
我在哪裏錯了代碼。如何解決這個問題。
在此先感謝。
而不是佈局= inflater.inflate(R.layout.alert_page,(ViewGroup中)findViewById(R.id.alert_Root)) ;寫佈局= inflater.inflate(R.layout.alert_page,null); – AkashG 2012-07-11 07:56:32
你正在調用對話框爲'showDialog(int)'?那麼爲什麼你寫這行'dialog.show();'在'onCreateDialog' – 2012-07-11 08:14:28
@MMohsinNaeem我可以刪除這行'dialog.show();'後我得到同樣的問題。 – Sekar 2012-07-11 08:31:59