将动态组件添加到Android中的自定义对话框

时间:2017-03-19 11:57:15

标签: android dialog dynamically-generated customdialog

我需要你的帮助在Android中做一些事情,在下面的用例中。 我在Android中创建了一个自定义对话框,其布局为: dialog_threshold.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp">
    <ImageView
        android:src="@drawable/ic_media_route_on_03_dark"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:background="#FFFFBB33"
        android:contentDescription="@string/set_target" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner_threshold" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layout_threshold">

        <TextView
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/targetSelected"
            android:layout_weight="1" />

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/threshold_operator_spinner"
            android:layout_weight="1" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text=""
            android:ems="10"
            android:id="@+id/threshold_val"
            android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

在Activity中,当我点击一个特定的按钮时,我有类似的东西,来创建自定义对话框:

setThreshold_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder mBuilder = new AlertDialog.Builder(DiseaseActivity.this);
                //Inflate the custom layout
                View mView = getLayoutInflater().inflate(R.layout.dialog_threshold, null);

                //Set title for dialog
                mBuilder.setTitle("Set thresholds for your target");
                //Define the spinner inside your custom layout
                final Spinner mSpinner = (Spinner) mView.findViewById(R.id.spinner_threshold); //because it doesn't exist in the main layout, but only in the custom layout
                //Define the ArrayAdapter
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(DiseaseActivity.this,
                        android.R.layout.simple_spinner_item,
                        getResources().getStringArray(R.array.threshold_choices));
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                mSpinner.setAdapter(adapter);

                if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
                    String ThresholdSelection = mSpinner.getSelectedItem().toString();
                }
                mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if ((dialog2 != null) && dialog2.isShowing() && !mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
                            dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);

                        } else {
                            dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

                //Set the positive and negative button for the custom dialog
                mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        disease.setThreshold(mSpinner.getSelectedItem().toString());

                        if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
                            Toast.makeText(DiseaseActivity.this,
                                    mSpinner.getSelectedItem().toString(),
                                    Toast.LENGTH_SHORT)
                                    .show();
                            setInterval_button.setEnabled(true);
                        }
                    }
                });
                mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                mBuilder.setView(mView);
                dialog2 = mBuilder.create();
                dialog2.show();
                dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

            }
        });

我现在要做的是在显示之前动态地将其他组件(如EditText或TextView)添加到上面的Dialog中。 我该怎么办?

提前致谢!

1 个答案:

答案 0 :(得分:2)

只需创建自定义视图&gt;&gt;找到父(要添加的视图组)&gt;&gt;&gt;并添加它。的例如: -

mView.addView(new TextView(ActivityContext),LayoutParamss);
mBuilder.setView(mView);