使用微调器作为对话标题

时间:2014-06-18 21:48:49

标签: android android-spinner android-alertdialog

我正在尝试创建一个AlertDialog(应该在单击Textvie练习时出现),其中包含一个微调器作为标题。当在微调器中选择了不同的项目时,它应该更改对话框列表的内容。

exercise = (TextView)findViewById(R.id.add_exerc);
exercise.setText("TEST");
initExerOCL();  //init the OnItem

private void initExerOCL(){
    exercise.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //diaTitle is used for the String[] from which to create the dialogList
            AlertDialog a = showListCheckPickerDialog(diaTitle);
            a.show();
        }
    });
}

public AlertDialog showListCheckPickerDialog(int i){
    mSelectedItems = new ArrayList();  //saves selected items

    LayoutInflater inflater = getLayoutInflater();
    View v = inflater.inflate(R.layout.add_dia_spinner_title, null);
    s = (Spinner) findViewById(R.id.add_dia_t);
    ArrayAdapter<CharSequence> adapterS = ArrayAdapter.createFromResource(this,
            R.array.trainings, R.layout.spinner_item);
    adapterS.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapterS);
    s.setOnItemSelectedListener(this);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.a_add)
    //set the View for the spinner
    .setCustomTitle(v)
    .setMultiChoiceItems(diaTitle, null,
            new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which,
                boolean isChecked) {
            if (isChecked) {
                // If the user checked the item, add it to the selected items
                mSelectedItems.add(which);
            } else if (mSelectedItems.contains(which)) {
                // Else, if the item is already in the array, remove it 
                mSelectedItems.remove(Integer.valueOf(which));
            }
        }
    })
    // Set the action buttons
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) { }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) { }
    });
    return builder.create();
}

我希望有人能够提供解决方案或更好的方法,因为这种方式对我来说似乎相当复杂(甚至不起作用)。谢谢!

2 个答案:

答案 0 :(得分:0)

使用“活动”代替“对话”,请参阅此帖子:Android Activity as a dialog或此部分文档:http://developer.android.com/guide/topics/ui/dialogs.html#ActivityAsDialog

然后使用您想要的内容实现微调器,在活动中设置下拉导航(有关详细信息,请参阅文档http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown

答案 1 :(得分:0)

只需以编程方式创建一个Spinner并将其添加到自定义标题中,而不是对布局进行充气。

<强>样品:

Spinner s2 = new Spinner(this);
    ArrayAdapter<CharSequence> adapterS = ArrayAdapter.createFromResource(this,
            R.array.trainings, R.layout.spinner_item);
    adapterS.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapterS);
    s.setOnItemSelectedListener(this);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("hello")
    //set the View for the spinner
    .setCustomTitle(s2)
    // Set the action buttons
    .setPositiveButton("okie", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) { }
    })
    .setNegativeButton("lawl", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) { }
    })