简体   繁体   English

扩展一个CustomDialog,edittext总是空的

[英]Extending a CustomDialog, edittext always empty

I am trying to add a custom field to IconDialog ( https://github.com/maltaisn/icondialoglib )我正在尝试向 IconDialog 添加自定义字段( https://github.com/maltaisn/icondialoglib

I added a new EditText into the xml and I am trying to access it in my activity onIconDialogIconsSelected (which is a Callback when the Select button of this dialog is pressed).我在 xml 中添加了一个新的 EditText,我试图在我的活动 onIconDialogIconsSelected 中访问它(这是按下此对话框的 Select 按钮时的回调)。

The editText.getText() is always empty but I can see it when debugging in the view. editText.getText() 始终为空,但在视图中调试时我可以看到它。

The new class:新的 class:

public class InputIconDialog extends IconDialog {
    private EditText editText;
    public InputIconDialog() {

    }

    public String getInputText() {
        View v = getLayoutInflater().inflate(R.layout.icd_dialog_icon, null);
        editText = v.findViewById(R.id.icd_edt_InputName); // this editText exists but is always empty
        return editText.getText().toString();
    }
}

The problem here is that you are not actually accessing the view on display.这里的问题是您实际上并没有访问显示的视图。 You inflated a new View on a null parent ViewGroup in this line View v = getLayoutInflater().inflate(R.layout.icd_dialog_icon, null);您在这一行的 null 父ViewGroup上膨胀了一个新View View v = getLayoutInflater().inflate(R.layout.icd_dialog_icon, null); . . Although view has been created it is not tied to anything UI wise.尽管已经创建了视图,但它并没有与任何 UI 相关联。

The other problem is that in IconDialog you already set the content of the dialog to the view dialog.setContentView(view);另一个问题是在IconDialog您已经将对话框的内容设置为视图dialog.setContentView(view); that you inflated there.你在那里膨胀了。


A simple solution would be to allow the children of IconDialog to change its base layout.一个简单的解决方案是允许IconDialog的子级更改其基本布局。

public class IconDialog extends DialogFragment {

    @LayoutRes
    protected int layoutRes = R.layout.icd_dialog_icon;

    Dialog onCreateDialog(final Bundle state) {
        LayoutInflater inflater = LayoutInflater.from(context);
        @SuppressLint("InflateParams") final View view = inflater.inflate(layoutRes, null);
}

public class InputIconDialog extends IconDialog {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        layoutRes = R.layout.icd_dialog_icon;
    }

A major drawback to such solution is its maintainability long term in the sense that the children of IconDialog need to include all the views it depends on in their layout with <inclue or replication.这种解决方案的一个主要缺点是它的长期可维护性,因为IconDialog的子项需要在其布局中使用<inclue或复制包含它所依赖的所有视图。


Recommendation推荐

I would use onCreateView in IconDialog to set the layout and views and then override it in InputIconDialog and call super.我会在IconDialog中使用onCreateView来设置布局和视图,然后在InputIconDialog中覆盖它并调用 super。 That plus 3 layout files, one for icon_dialog, one for the shared views between the 2 dialogs and one for input_icon_dialog.再加上 3 个布局文件,一个用于 icon_dialog,一个用于 2 个对话框之间的共享视图,一个用于 input_icon_dialog。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM