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

标签 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)。 我该怎么做?

提前致谢!

最佳答案

只需创建您的自定义 View >> 找到父 View (您要添加的 View 组) >>> 并添加它。 例如:-

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

关于android - 将动态组件添加到 Android 中的自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42886291/

相关文章:

javascript - 在 React 中动态生成的链接之外的 html 链接之间添加项目符号 (•)

c - 动态分配的矩阵不允许输入

java - Android 应用程序中无法包含 "javaee-api-7.0.jar"

android - 我需要其他方法只有在我按下对话框中的任何按钮后才能运行

forms - 清除p :dialog on close after failed action/method call中的表格

Android 在警报对话框中设置文本

java.lang.IllegalStateException : Observer android. widget.AbsListView$AdapterDataSetObserver 未注册

android - 在android系统上测试屏幕

android - 使用 Firebase 将图像检索到 RecyclerView 时无法创建 Firebase 存储网络请求 android.os.RemoteException

android - 从图像到我的Android手机到blob mysql远程数据库