繁体   English   中英

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

[英]Why custom dialog box button is not working in 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)

我在哪里弄错了代码。 如何解决这个问题。

提前致谢。

这样做:

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

对按钮的单击事件做您想做的任何事情。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM