Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

android - Closing a custom alert dialog on button click

I'm having trouble closing my alert dialog. I am using a layout inflator to make the dialog, so I'm not sure how I would go about closing the thing after I'm done with it. Code is below:

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();

//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);

add_item.setOnClickListener(new OnClickListener()
{
        @Override
        public void onClick(View v)
        {
        //Need this to close the alert dialog box
        }
});

title.setText(workout_items[position]);
dialog.setView(layout);
dialog.show();

I cant call finish, because that closes the listview that the alert dialog is launched from, and the dialog.dismiss calls are not available to me.

What do you think I should do to fix this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();

//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);

title.setText(workout_items[position]);
dialog.setView(layout);

AlertDialog alertDialog = dialog.create();

add_item.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        alertDialog.dismiss();
    }
});


alertDialog.show();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...