setSingleChoiceItems和setPositiveButton Android

时间:2016-12-26 01:03:55

标签: android dialog multiple-choice

有没有办法将多个复选框变成只能在一次检查一个框的框?

所以这是我想要解决的问题,包括对话框警告框和单一选择 - 多选。

public class MainActivity extends Activity {

    CharSequence[] items = {"FIT", "Fakultet za menadzment", "Fakultet digitalnih umetnosti"};
    private String selectedItem;
    public int proba;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    private void launchWebPage(String url){
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }

    public void dialog(View v){
        showDialog(0);
    }

    public void dialog1(View v){
        Toast.makeText(getBaseContext(), "Nema deklarisanu funkciju.", Toast.LENGTH_LONG).show();
    }

    public void dialog2(View v){
        Toast.makeText(getBaseContext(), "Nema deklarisanu funkciju.", Toast.LENGTH_LONG).show();
    }


    public Dialog onCreateDialog(final int id) {

                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Odabir");
                builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                selectedItem = (String) items[i];
                                proba = i;


                            }
                        });
                        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int position) {

                                Toast.makeText(getBaseContext(), "Test", Toast.LENGTH_SHORT).show();

                            }
                        });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getBaseContext(), "Cancel pritisnuto!", Toast.LENGTH_SHORT).show();
                    }
                });

                builder.show();

        return null;
    }
}

3 个答案:

答案 0 :(得分:2)

您需要使用radioButon而不是checkbox

https://developer.android.com/guide/topics/ui/controls/radiobutton.html

答案 1 :(得分:2)

使用其中一种setSingleChoiceItems() on AlertDialog.Builder。尽管文档说的是,这应该使用单选按钮,而不是复选框。

答案 2 :(得分:0)

最后想通了,如果有人需要类似的东西,这就是缺少的代码。

public Dialog onCreateDialog(final int id) {

            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Odabir");
            builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            selectedItem = (String) items[i];
                            proba = i;

                        }
                    });
                    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int position) {

                            ListView lw = ((AlertDialog) dialog).getListView();
                            Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());
                            Toast.makeText(getApplicationContext(), ((String) checkedItem), Toast.LENGTH_SHORT).show();

                            if(proba == 0){
                                launchWebPage("http://www.metropolitan.ac.rs/osnovne-studije/fakultet-informacionih-tehnologija/");
                            } else if(proba == 1){
                                launchWebPage("http://www.metropolitan.ac.rs/osnovne-studije/fakultet-za-menadzment/");
                            } else {
                                launchWebPage("http://www.metropolitan.ac.rs/fakultet-digitalnih-umetnosti-2/");
                            }

                        }
                    });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(getBaseContext(), "Cancel pritisnuto!", Toast.LENGTH_SHORT).show();
                }
            });

            builder.show();

    return null;
}