Android onClick on positive按钮

时间:2013-07-24 14:15:10

标签: android button click

我在onClick监听器上遇到小问题。 我有一个包含正面和负面按钮的对话框,正面按钮应该像这样:

if(statement == true) - >点击肯定按钮会做一些事情 if(statement == false) - >单击肯定按钮将显示Toast.makeText(),但这不会关闭对话框窗口。

提前致谢!

2 个答案:

答案 0 :(得分:0)

您需要调用close方法才能关闭它。把这一行放在pos / neg按钮监听器的最后一行:

dialog.cancel();

其中dialog是DialogInterface的实例,它作为参数传递。在

DialogInterface.OnClickListener.onClick(DialogInterface ,int);

更新

builder.setNeutralButton( "Ustaw", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                        getRecurrence( whichType, finishType );
                        if( isRecurrenceProperlySet ) {
                                //do some stuff

                        }
                        else {
Toast.makeText( contextThemeWrapper, "Set data!", Toast.LENGTH_SHORT ).show();
}
 dismiss();
                }
        });

正如我之前所说,将该行移动到侦听器的最后一行。

答案 1 :(得分:0)

一旦尝试这个

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);
    findViewById(R.id.test).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            show();
        }
    });

}

AlertDialog alertDialogBox;
private OnClickListener clickListner = new OnClickListener() {
    @Override
    public void onClick(View v) {
        show();
    }
};
private boolean conditionVal = false;

public void show() {

    AlertDialog.Builder b = new AlertDialog.Builder(this);
    alertButtonValue = false;
    b.setNegativeButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    b.setPositiveButton("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    this.alertDialogBox = b.create();
    this.alertDialogBox.setTitle("Helllo");
    this.alertDialogBox.setMessage("message...");

    alertDialogBox.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            if (alertButtonValue == false) {
                Button button = alertDialogBox
                        .getButton(DialogInterface.BUTTON_NEGATIVE);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!conditionVal)
                            Toast.makeText(getApplicationContext(),
                                    "Condition not satisfied",
                                    Toast.LENGTH_SHORT).show();
                    }
                });
                alertButtonValue = true;
            } else {
                Toast.makeText(getApplicationContext(), "Hello",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
    this.alertDialogBox.show();
}

private boolean alertButtonValue = false;