为什么自定义对话框按钮没有在Android平台 [英] Why custom dialog box button is not working in android
问题描述
我们已经创建了两个自定义对话框,一个是关于,另一个是警报。当我在那个时候拖自定义对话框交替选择按钮不起作用。
We have creating two custom dialog box, one is about and another one is alert. When i choose alternately in the tow custom dialog box at that time the button is not working.
样code
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;
}
例如我正在使用两个按钮。第一个按钮叫案例1
和第二个按钮被称为 2的情况下
。
For example i am using two button. First button is called case 1
and second button is called case 2
.
我选择的第一个按钮访问案例1
然后再选择自定义对话框 alertUser
按钮成功退出对话框
。
I am selected First button to access case 1
and then selected custom dialog box alertUser
button successfully Exit the dialog box
.
随即我选择第二个按钮访问案例2
,然后选择自定义对话框 aboutUser
按钮成功退出对话框
。
Immediately I am selected Second button to access case 2
and then select custom dialog box aboutUser
button successfully Exit the dialog box
.
刚结束后,我选择的第一个按钮访问案例1
,然后选择自定义对话框 alertUser
按钮现在对话框中不存在(现在的按钮不工作)
。
After Immediately I am selected First button to access case 1
and then select custom dialog box alertUser
button Now the dialog box does not exist (button is now not working)
.
我在哪里错了code。如何解决这个问题。
Where i am mistaken code. How do solved this problem.
先谢谢了。
推荐答案
您呼叫对话框,的ShowDialog(INT)
。删除此行 dialog.show();
在 onCreateDialog
you are calling dialog as showDialog(int)
. remove this line dialog.show();
in your onCreateDialog
和呼叫 dismissDialog(INT)
来关闭该对话框,而不是 dialog.dismiss();
and call dismissDialog(int)
to dismiss the dialog instead of dialog.dismiss();
这篇关于为什么自定义对话框按钮没有在Android平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!