我们创建了两个自定义对话框,一个是关于,另一个是警报。当我当时在自定义对话框中交替选择时,按钮不起作用。
示例代码
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
。
我选择了 First button to access case 1
,然后选择了自定义对话框alertUser
按钮successfully Exit the dialog box
。
我立即选择了 Second button to access case 2
,然后选择 custom dialog box aboutUser
button successfully Exit the dialog box
。
之后我立即选择了 First button to access case 1
,然后选择 custom dialog box alertUser
button Now the dialog box does not exist (button is now not working)
。
我在哪里弄错了代码。这个问题怎么解决的。
提前致谢。