AlertDialog inside alertdialog android

后端 未结 4 2154
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-18 07:25

I am trying to add an alertdialog within an alertdialog.But not able to see the second alertdialog..please help me here is my code shown

AlertDialog alertDia         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-18 08:06

    import android.widget.TextView;
    import android.widget.Button;
    import android.view.View;
    import android.content.DialogInterface;
    
    public class MainActivity extends AppCompatActivity {
        Button click;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            click=(Button)findViewById(R.id.btnId);
    
            click.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    txtSubmit();
                }
            });
        }
    
    
    protected void txtSubmit(){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("This is alert title");
            builder.setMessage("This is message for Alert dialog");
            builder.setPositiveButton("Yup", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
                    builder1.setTitle("This is alert title inside");
                    builder1.setMessage("This is message for Alert dialog inside");
                    builder1.show();
                }
            });
            builder.show();
        }
    }
    

    The txtSubmit function calling from the onClick event listener. Also for the second alert dialog need to open I have to pass

    AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
    

    Here, we have to bind the this event using className

    When we click on the yup button in the first dialog,it shows the second dialog that insides in the first one

提交回复
热议问题