如何从此Dialogplus库获取EditText值?

时间:2018-03-09 09:53:27

标签: android

我使用此库dialogplus https://github.com/orhanobut/dialogplus在顶部显示自定义视图。

现在我想获取EditText值,但是我得到一个错误的null对象。

我看到他们谈论如何获得价值:

对于customview holder,有一种特定方法可以恢复您的视图。然后,您可以使用findViewById并访问edittext或其他输入。

View view = dialogPlus.getHolderView();

但我不知道如何使用这段代码......

我的全球

private Button searchButton;

这是我的代码:

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) {

 presenterEdit = (EditText) view.findViewById(R.id.searchButton);

                    switch (view.getId()) {
                        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();
}

我这样试试,它无效:

@Override
                public void onClick(DialogPlus dialog, View view) {

                    view = dialog.getHolderView();
                    presenterEdit = (EditText) view.findViewById(R.id.searchButton);

                    switch (view.getId()) {
                        case R.id.searchButton:

                            String testString = presenterEdit.getText().toString();

                            Toast.makeText(getActivity(), testString, Toast.LENGTH_SHORT).show();

                            break;
                    }
                }

这是我的search_layout:

<?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/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>

3 个答案:

答案 0 :(得分:3)

您应该创建EditText,然后将其分配给您在xml中指定的ID:

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) {
                //dialog.getHolderView().findViewById(...)
                EditText presenterEdit = (EditText)dialog.getHolderView().findViewById(R.id.presenterEdit);


                    switch (view.getId()) {
                        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();
}

答案 1 :(得分:2)

试试这个

    View root = dialog.getHolderView();
    EditText presenterEdit  =  root.findViewById(R.id.presenterEdit);
    String text = presenterEdit.getText().toString();
希望这会奏效。

答案 2 :(得分:1)

您可以使用此库 点击here 到那时你可以做到这一点

 CustomLayoutDialog dialog = new DialogPlusBuilder().buildCustomLayoutDialog(R.layout.search_layout);
                dialog.showNow(getChildFragmentManager(), "search_layout");
 SearchLayoutBinding=(SearchLayoutBinding) dialog.getCustomLayoutBinding();
 languageDialogBinding.presenterEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                          //TDOD implement here 
                    }
                }
            });