问题
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.
Sample 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;
}
For example i am using two button. First button is called case 1 and second button is called case 2.
I am selected First button to access case 1 and then selected custom dialog box alertUser button successfully Exit the dialog box.
Immediately I am selected Second button to access case 2 and then select custom dialog box aboutUser button successfully Exit the dialog box.
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).
Where i am mistaken code. How do solved this problem.
Thanks in advance.
回答1:
you are calling dialog as showDialog(int). remove this line dialog.show(); in your onCreateDialog
and call dismissDialog(int) to dismiss the dialog instead of dialog.dismiss();
回答2:
Do like this:
Button button1=(Button) findViewById(R.id.btn1);
Button button2=(Button) findViewById(R.id.btn2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
implement your Activity by OnClickListener and add unimplemented method which u will get as onClick.
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
//write code
break;
case R.id.btn2:
//write code
break;
}
}
Do whatever you want to do on click event of button.
来源:https://stackoverflow.com/questions/11427925/why-custom-dialog-box-button-is-not-working-in-android