为什么自定义对话框按钮在android中不起作用

时间:2012-07-11 07:46:05

标签: android dialog

我们创建了两个自定义对话框,一个是关于,另一个是提醒。当我在拖曳自定义对话框中交替选择时,该按钮不起作用。

示例代码

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)

我错误的代码。怎么解决这个问题。

提前致谢。

2 个答案:

答案 0 :(得分:0)

这样做:

Button button1=(Button) findViewById(R.id.btn1);
Button button2=(Button) findViewById(R.id.btn2);

 button1.setOnClickListener(this);
 button2.setOnClickListener(this);

通过OnClickListener实现您的Activity并添加未实现的方法,您将获得onClick。

public void onClick(View v) {
switch(v.getId()){
        case R.id.btn1:
            //write code
            break;
        case R.id.btn2:
            //write code
            break;
        }
}

点击按钮事件时,您可以执行任何操作。

答案 1 :(得分:0)

您正在将对话框称为showDialog(int)。在dialog.show();

中删除此行onCreateDialog

并致电dismissDialog(int)以取消对话而不是dialog.dismiss();