android– 如何从这个Dialogplus库中获取EditText值?

android– 如何从这个Dialogplus库中获取EditText值?,第1张

概述我使用这个库dialogplushttps://github.com/orhanobut/dialogplus在顶部显示自定义视图.现在我想获取EditText值,但是我得到一个错误的null对象.我看到他们谈论如何获得价值:对于customviewholder,有一种特定的方法可以恢复您的视图.然后,您可以使用findViewById并访问edittext

我使用这个库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>

解决方法:

您应该创建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();}
总结

以上是内存溢出为你收集整理的android – 如何从这个Dialogplus库中获取EditText值?全部内容,希望文章能够帮你解决android – 如何从这个Dialogplus库中获取EditText值?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1108891.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存