单击“对话框”按钮后设置按钮的可见性

时间:2016-01-05 00:17:50

标签: android visibility android-alertdialog android-button

我有一个带按钮的布局。

The layout

onclick显示AlertDialog,其中包含Ok和Cancel选项。我希望如果单击"确定",布局中的按钮就会消失。

到目前为止,这是我的代码:

总的来说,我有这个功能:

public void requestCheckButton(View view) {
    RequestAccepted ra = new RequestAccepted(this);
}

RequestAccepted函数:

public class RequestAccepted {
Context context;

public RequestAccepted(final Context context){
    this.context = context;

    final AlertDialog.Builder popDialog = new AlertDialog.Builder(context);
    final SeekBar seek = new SeekBar(context);
    seek.setMax(11); // Para tener incrementos de 5 min
    seek.setProgress(1);

    popDialog.setTitle("¿En cuánto tiempo puedes llegar?");
    popDialog.setMessage("5 min");
    popDialog.setView(seek);
    popDialog.setPositiveButton("Mandar", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(context, "Presionaste Mandar", Toast.LENGTH_SHORT).show();

        }
    });

    final AlertDialog dialog = popDialog.create();
    seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progress = 1;

        public void onProgressChanged(SeekBar seekBar, int progressV, boolean fromUser) {
            progress = progressV;
        }

        public void onStartTrackingTouch(SeekBar arg0) {

        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            //Por cada incremento se suman 5 min
            dialog.setMessage((progress)*5 +" min");
        }
    });
    dialog.show();
}

1 个答案:

答案 0 :(得分:1)

在RequestAccepted类中添加Button button作为参数:

public RequestAccepted(final Context context, final Button button) {
    ...
}

在对话框的ok按钮的onClickListener中修改它:

popDialog.setPositiveButton("Mandar", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(context, "Presionaste Mandar", Toast.LENGTH_SHORT).show();
            button.setVisibility(View.GONE);
        }
    });

现在从您想要的任何活动中调用此类,如下所示:

public void requestCheckButton(View view){
    RequestAccepted ra = new RequestAccepted(this, button);
}

其中button是您要将其可见性设置为已消失的按钮。