【问题标题】:Use a Spinner as Dialog Title使用微调器作为对话框标题
【发布时间】:2014-06-18 21:48:48
【问题描述】:

我正在尝试创建一个 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();
}

我希望现在有人找到解决方案或更好的方法,因为这种方法对我来说似乎很复杂(甚至行不通)。谢谢!

【问题讨论】:

    标签: android android-spinner android-alertdialog


    【解决方案1】:

    只需以编程方式创建一个 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) { }
        })
    

    【讨论】:

      【解决方案2】:

      使用 Activity 而不是 Dialog 请参阅此帖子: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)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多