如何在弹出窗口中恢复DialogFragment中的参数

时间:2013-02-08 09:39:16

标签: android android-layout popup android-alertdialog android-dialogfragment

我正在尝试像男人那样建立连接弹出窗口: http://developer.android.com/guide/topics/ui/dialogs.html

像男人这样的例子已经完成,但现在我无法恢复用户填写的登录名和密码。

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center"
        android:contentDescription="@string/logo"
        android:scaleType="center"
        android:src="@drawable/logo" />

    <EditText
        android:id="@+id/edtlogin"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="@string/identifiant" />
    <EditText
        android:id="@+id/edtpassword"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"
        android:fontFamily="sans-serif"
        android:hint="@string/motdepasse"/>
</LinearLayout>

    public class PopUpConnexion extends DialogFragment {

    private EditText login;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
        // Get the layout inflater
        LayoutInflater inflater = this.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.popup_connexion, null))
        // Add action buttons
               .setPositiveButton(R.string.connexion, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {


// Here I want to recover the login and the password for sign in.


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

}

我尝试通过各种方式恢复值,但EditText类型的对象登录始终为null。 感谢

编辑:

    public class PopUpConnexion extends DialogFragment {

    EditText login;
    EditText password;


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

        View textEntryView = inflater.inflate(R.layout.popup_connexion, null);
        login  = (EditText) textEntryView.findViewById(R.id.edtlogin); // with the debugger we can see it's the good EditText with the id
        login.setText("test"); // we can't see "test" in the EditText


        builder.setView(inflater.inflate(R.layout.popup_connexion, null))
        // Add action buttons
               .setPositiveButton(R.string.connexion, new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {

                            String tmp = "login: " + login.getText().toString() + "|"; // with the debugger we can see it's the good EditText with the id
                          Toast.makeText(getActivity().getApplication().getApplicationContext(), tmp, Toast.LENGTH_SHORT).show();
                   }
               })
               .setNegativeButton(R.string.annuler, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       PopUpConnexion.this.getDialog().cancel();
                   }
               });     
        return builder.create();
    }

}

使用login.setText(“test”);我们可以得出结论,可以看到的视图与我得到的文本().Text()的视图不同。

最终编辑:

    public class PopUpConnexion extends DialogFragment {

    EditText identifiant;
    EditText motdepasse;
    Button Connexion;

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

        View textEntryView = inflater.inflate(R.layout.popup_connexion, null);
        login = (EditText) textEntryView.findViewById(R.id.edtlogin);
        password = (EditText) textEntryView.findViewById(R.id.edtpassword);
        Connexion = (Button) textEntryView.findViewById(R.id.loginButton);


        Connexion.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity().getApplication().getApplicationContext(), login.getText() + " " + password.getText(), Toast.LENGTH_SHORT).show();
            }
        });

        builder.setView(textEntryView);
        return builder.create();
    }

}

最后,我不像男人那样使用事件管理器。 我已经测试了这个技术,但是由于当你编写OnClickListener时DialogFragment.OnClickListener和View.OnClickListener之间的混淆,我遇到了一些错误。 现在它的工作,但我认为使用builder.setPositiveButon()&amp; amp存在一个更好的方法。共

1 个答案:

答案 0 :(得分:-1)

你想要显示对话框frnd。你想要的那种类型