positiveButton,negativeButton和neutralButton在alertdialog android中被禁用

时间:2016-01-14 15:17:43

标签: android alertdialog

我想知道为什么在警告对话框中禁用所有按钮。它工作得非常好,突然之间一切都很好。

我的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

switch (id) {

    case R.id.generate_pdf_2:
        //

        /*new AlertDialog.Builder(getActivity()).setTitle("Criando Recibo")
                .setMessage("Tem certeza que deseja criar o recibo?")
                .setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((AlertDialog)dialog).getButton(which).setVisibility(View.VISIBLE);
                        GeradorRecibo geradorRecibo = new GeradorRecibo(getActivity(), cliente);
                        Toast.makeText(getActivity(), "Recibo gerado com sucesso!", Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("Não", null)
                .show();*/

//我试试这个,但在两个例子中,AlertDialog都不起作用。

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Criando Recibo");
        builder.setMessage("Deseja criar o recibo?");
        builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                GeradorRecibo geradorRecibo = new GeradorRecibo(getActivity(), cliente);
                Toast.makeText(getActivity(), "Recibo gerado com sucesso!", Toast.LENGTH_LONG).show();
            }

        });

        AlertDialog dialog = builder.create();
        final Button btn = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
        btn.setEnabled(true);
        dialog.show();
        return true;

我为我的英语而烦恼

1 个答案:

答案 0 :(得分:0)

试试这个:

AlertDialog.Builder alert = new AlertDialog.Builder( getActivity() );
AlertDialog  dialog =  alert.create();
alert.setTitle( "My title" );
alert.setPositiveButton( "Ok",  new AlertDialog.OnClickListener() {
      public void onClick( DialogInterface dialog, int whichButton ) {  
            //Do something
      }
});
alert.setNegativeButton("KO", new AlertDialog.OnClickListener(){
      public void onClick( DialogInterface dialog, int whichButton ) {  
            //Do something
      }
});
alert.show();   

希望它有所帮助。