为什么自定义对话框按钮在android中不起作用
我们创建了两个自定义对话框,一个是关于,另一个是警报。当我在tow custom(牵引自定义)对话框中选择Alternate(交替)时,按钮不起作用 示例代码为什么自定义对话框按钮在android中不起作用,android,dialog,Android,Dialog,我们创建了两个自定义对话框,一个是关于,另一个是警报。当我在tow custom(牵引自定义)对话框中选择Alternate(交替)时,按钮不起作用 示例代码 AlertDialog.Builder builder; Context mContext; LayoutInflater inflater; View layout; Dialog dialog; @Override protected Dialog onCreateDialog( int id ) { switch ( i
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;
}
例如,我使用两个按钮。第一个按钮称为case1
,第二个按钮称为case2
我首先选择按钮访问案例1
,然后选择自定义对话框alertUser
按钮成功退出对话框
立即选择第二个按钮访问案例2
,然后选择自定义对话框关于用户
按钮成功退出对话框
立即选择我后,首先单击按钮访问案例1,然后选择自定义对话框警报用户按钮现在对话框不存在(按钮现在不工作)
我把代码弄错了。你是如何解决这个问题的
提前感谢。请这样做:
Button button1=(Button) findViewById(R.id.btn1);
Button button2=(Button) findViewById(R.id.btn2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
通过OnClickListener实现您的活动,并添加未实现的方法,该方法将作为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.dismise()代码>代替布局=充气机。充气(R.layout.alert\u页面,(视图组)findViewById(R.id.alert\u根));写入布局=充气机。充气(R.layout.alert_页,空);您正在以showDialog(int)
的身份调用dialog?那么为什么要写这行dialog.show()
在onCreateDialog
@mmohsnaeem中,我可以删除这一行dialog.show()代码>在我遇到同样的问题后。删除它..并调用dismissDialog(int)
以关闭对话框,而不是dialog.dismise()代码>@mmohsineem谢谢。很好用。请解释为什么我使用dialog.discouse()时旧代码不起作用代码>我试图访问这行代码时收到错误消息Button button2=(Button)findViewById(R.id.btn2)代码>错误消息是类型视图中的方法setOnClickListener(View.OnClickListener)不适用于参数
在活动的onCreate中初始化两个按钮,然后设置它们的click listener.clean并生成项目。当您再次卡住时询问Hanks。但是showlialog(int)
和dismisslialog(int)
是不推荐使用的。如何解决这个问题。我被推荐到developer.android.com
,但我不知道。你能给我举个例子吗。。谢谢