在这种情况下打开布局时如何设置微调器项目?

时间:2018-03-12 03:56:39

标签: android android-dialog

我使用此库https://github.com/orhanobut/dialogplus

当我点击按钮时,我将打开包含EditTextSpinner的对话框布局。

我的问题是,当对话框布局打开时,我不知道如何设置Spinner项。这个库让我在Spinner下宣布我的setOnClickListener,因为我必须使用dialog.getHolderView()来获取视图。我认为这是问题,但我不知道如何解决它。

在我的代码中,我必须点击Spinner,然后我的微调器项目才会被添加。

这是我的代码:

public void openSearchLayout() {
    DialogPlus dialog = DialogPlus.newDialog(getActivity())
            .setContentHolder(new ViewHolder(R.layout.search_layout))
            .setGravity(Gravity.TOP)
            .setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(DialogPlus dialog, View view) {

                    EditText presenterEdit = (EditText) dialog.getHolderView().findViewById(R.id.presenterEdit);
                    final Spinner searchDate = (Spinner) dialog.getHolderView().findViewById(R.id.searchDate);
                    // Clear spinner data
                    dateList.clear();
                    // Set spinner item function
                    showRoute(myJson, searchDate);
                    switch (view.getId()) {
                        case R.id.searchDate:
                            searchDate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    // Clear spinner data
                                    arrayList.clear();
                                    // Search the spinner item value list
                                    showDateRoute(myJson, searchDate.getSelectedItem().toString());
                                }
                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {
                                }
                            });
                            break;

                        case R.id.searchButton:
                            String testString = presenterEdit.getText().toString();
                            Toast.makeText(getActivity(), testString, Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            })
            .setExpanded(true, 900)  // This will enable the expand feature, (similar to android L share dialog)
            .create();
    dialog.show();
}

这是我对话框的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingTop="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/signUpDate"
            android:textColor="@android:color/black"
            android:textSize="18dp" />

        <Spinner
            android:id="@+id/searchDate"
            android:layout_width="match_parent"
            android:layout_height="38dp"
            android:layout_gravity="center"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/spinner_arrow"
            android:spinnerMode="dropdown"
            android:theme="@style/mySpinnerStyle" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingTop="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/circlePresenter"
            android:textColor="@android:color/black"
            android:textSize="18dp" />

        <EditText
            android:id="@+id/presenterEdit"
            android:layout_width="match_parent"
            android:layout_height="38dp"
            android:layout_gravity="center"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/search_edit"
            android:spinnerMode="dropdown" />

    </LinearLayout>

    <Button
        android:id="@+id/searchButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:background="@drawable/corner_search"
        android:text="@string/integralDateSearch"
        android:textColor="@android:color/white"
        android:textSize="20dp" />

</LinearLayout>

任何帮助将不胜感激。提前谢谢。

根据Reaz的回复,我更新如下:

ViewHolder dialogViewHolder = new ViewHolder(R.layout.search_layout);
LayoutInflater inflater =LayoutInflater.from(getActivity());
final EditText presenterEdit = (EditText) dialogViewHolder.getView(inflater, null).findViewById(R.id.presenterEdit);
final Spinner searchDate = (Spinner) dialogViewHolder.getView(inflater, null).findViewById(R.id.searchDate);
// Clear spinner data
dateList.clear();
// Set spinner item function
showRoute(myJson, searchDate);

DialogPlus dialog = DialogPlus.newDialog(getActivity())
        .setContentHolder(dialogViewHolder)
        //.....

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作,从对话框初始化中获取视图。首先初始化视图,填充微调器,然后将其传递给ViewHolder的构造函数中的对话框。

我测试了它。它适用于我的设备。

public void openSearchLayout() {
    View view = getLayoutInflater().inflate(R.layout.search_layout, null);
    final EditText presenterEdit = (EditText) view.findViewById(R.id.presenterEdit);
    final Spinner searchDate = (Spinner) view.findViewById(R.id.searchDate);
    // Clear spinner data
    dateList.clear();
    // Set spinner item function
    showRoute(searchDate);

    DialogPlus dialog = DialogPlus.newDialog(MainActivity.this)
            .setContentHolder(new ViewHolder(view))
            .setGravity(Gravity.TOP)
            .setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(DialogPlus dialog, View view) {
                    switch (view.getId()) {
                        case R.id.searchDate:
                            searchDate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    // Clear spinner data
                                    //arrayList.clear();
                                    // Search the spinner item value list
                                    //showDateRoute(myJson, searchDate.getSelectedItem().toString());
                                }

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

                        case R.id.searchButton:
                            String testString = presenterEdit.getText().toString();
                            Toast.makeText(MainActivity.this, testString, Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            })
            .setExpanded(true, 900)  // This will enable the expand feature, (similar to android L share dialog)
            .create();
    dialog.show();
}

我使用以下函数填充了微调器中的项目。只需添加一个示例showRoute函数来检查项目是否正确填充。

public void showRoute(Spinner spinner) {
    List<String> spinnerArray = new ArrayList<String>();
    spinnerArray.add("item1");
    spinnerArray.add("item2");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, spinnerArray);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}