如何使用xml-resourcefile中的视图在DialogFragment中检索editText1 [英] How to retrieve editText1 in DialogFragment using view from xml-resourcefile

查看:78
本文介绍了如何使用xml-resourcefile中的视图在DialogFragment中检索editText1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在此DialogFragment中检索editText1?它存在于vraag_banen.xml中,但getView()为空.

How to retrieve editText1 in this DialogFragment? It exists in vraag_banen.xml but getView() is null.

对话框显示正常,也没有编译错误,但是我无法弄清楚如何使用xml资源文件中的自定义视图编写PositiveButton的事件处理程序.

The dialog shows fine, also no compilation errors, however I cannot figure out how to write the eventhandler for the PositiveButton, using a custom view from an xml resourcefile.

package mypackage;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

public class VraagBanenDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.vraag_banen, null))
        // Add action buttons
               .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {

                       Activity pa = getActivity().getParent(); //-> TabsMenu
                       Context bc = getActivity().getBaseContext(); //-> ContextImpl
                       View vi = getView(); //-> null
                       Class<? extends OnClickListener> cl = this.getClass(); //-> Class (nl.computerhuys.tabnavui.VraagBanenDialogFragment$1)
                       String acn = getActivity().getClass().getName(); //-> nl.computerhuys.tabnavui.Spel
                       String cn = getClass().getName(); //-> nl.computerhuys.tabnavui.VraagBanenDialogFragment$1
                       OnClickListener t = this; //-> VraagBanenDialogFragment$1

                       EditText editText1 = (EditText) getView().findViewById(R.id.editText1); 
                       EditText editText2 = (EditText) getView().findViewById(R.id.editText2);
                       int baan1 = Integer.valueOf(editText1.getText().toString());
                       int baan2 = Integer.valueOf(editText2.getText().toString());
                       InitSpel.addBaanNummer(baan1);
                       InitSpel.addBaanNummer(baan2);

                   }
               })
               .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       VraagBanenDialogFragment.this.getDialog().cancel();
                   }
               });      
        return builder.create();
    }

推荐答案

找到了一种方法:命名匿名视图:

found a way: name the anonymous view:

View v = inflater.inflate(R.layout.vraag_banen, null) 

,然后使用v而不是getView()来引用视图:

and then use v to refer to the view, instead of getView():

EditText editText1 = (EditText) v.findViewById(R.id.editText1).

等等.那行得通.

这篇关于如何使用xml-resourcefile中的视图在DialogFragment中检索editText1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆