Android:禁用setPositiveButton,除非和选择项目

时间:2014-11-23 06:25:01

标签: android android-dialog

我有一个代码[发布在下面的正文中],它使用“确定”和“取消”按钮显示数组列表中的项目列表。基本上我想禁用确定按钮,直到从列表中选择特定项目。任何人都可以告诉我如何为我的代码执行此操作吗?

代码:

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    final CharSequence myList[] = { "Tea", "Coffee", "Milk" };
    ArrayList<Integer> selList=new ArrayList<Integer>();
    boolean bl[] = new boolean[myList.length];
    RelativeLayout rl;
    String msg ="";
    final AlertDialog.Builder ad = new AlertDialog.Builder(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rl = (RelativeLayout) findViewById(R.id.myRL);
        final Button btn = (Button) findViewById(R.id.btnDialog);
        ad.setTitle("What do you Like ?");
        ad.setMultiChoiceItems(myList,bl, new OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
                if(arg2)
                {
                    selList.add(arg1);
                }
                else if (selList.contains(arg1))
                {
                    selList.remove(Integer.valueOf(arg1));
                }
            }
        });
        ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                msg="";
                for (int i = 0; i < selList.size(); i++) {

                    msg=msg+"\n"+(i+1)+" : "+myList[selList.get(i)];
                }

                Toast.makeText(getApplicationContext(),
                        "Total "+ selList.size() +" Items Selected.\n"+ msg , Toast.LENGTH_LONG)     
                        .show();
            }
        });
        ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(),
                        "You Have Cancel the Dialog box", Toast.LENGTH_LONG)     
                        .show();
            }
        });
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                msg="";
                ad.show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用getButton(Dialog.BUTTON_POSITIVE)从AlertDialog访问“确定”按钮。修改启动对话框的OnClickListener,以便在显示对话框之前禁用“确定”按钮:

btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        msg="";
        AlertDialog dialog = ad.create();
        Button okButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
        okButton.setEnabled(false);
        dialog.show(); // call show on the AlertDialog, not the Builder
    }
});

我不确定启用按钮需要发生什么,但如果它是对用户选择特定项目的响应,那么你应该在OnMultiChoiceClickListener中启用代码:

ad.setMultiChoiceItems(myList,bl, new OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
        if(arg2)
        {
            selList.add(arg1);
        }
        else if (selList.contains(arg1))
        {
            selList.remove(Integer.valueOf(arg1));
        }
        if ( /* YOUR CODE HERE */ ) {
            AlertDialog dialog = (AlertDialog) arg0;
            Button okButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
            okButton.setEnabled(true);
        }
    }
});