1

男性のように接続ポップアップを作成しようとしています: 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"); を使用 見ることができるビューは、私が getText() を取得したビューと同じではないと結論付けることができます。

最終編集:

    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() & co を使用したより良い方法が存在すると思います。

4

1 に答える 1

-1

ダイアログ ボックス frnd を表示します。欲しいタイプ

于 2013-02-08T09:44:05.870 に答える