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



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

当我单击一个按钮时,我将打开一个对话框布局,其中包括EditTextSpinner

我的

问题是我不知道如何在对话框布局打开时设置我的Spinner项目。这个库让我在setOnClickListener下声明我的Spinner,因为我必须使用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)
        //.....

您可以尝试以下操作,使视图退出对话框初始化。首先初始化视图,填充微调器,然后将其传递给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);
}

相关内容

  • 没有找到相关文章

最新更新