0

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

示例代码

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 aboutUserbutton successfully Exit the dialog box

之后我立即选择了 First button to access case 1,然后选择 custom dialog box alertUserbutton Now the dialog box does not exist (button is now not working)

我在哪里弄错了代码。这个问题怎么解决的。

提前致谢。

4

2 回答 2

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;
        }
}

在按钮的点击事件上做任何你想做的事情。

于 2012-07-11T07:54:54.447 回答
0

您将对话框称为showDialog(int). 删除此dialog.show();onCreateDialog

并调用dismissDialog(int)以关闭对话框而不是dialog.dismiss();

于 2012-07-11T10:55:20.377 回答