|
| 1 | +package com.vogella.android.retrofitgithub; |
| 2 | + |
| 3 | +import android.app.Dialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.DialogInterface; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.support.annotation.NonNull; |
| 8 | +import android.support.v4.app.DialogFragment; |
| 9 | +import android.support.v7.app.AlertDialog; |
| 10 | +import android.view.View; |
| 11 | +import android.widget.EditText; |
| 12 | + |
| 13 | +import okhttp3.Credentials; |
| 14 | + |
| 15 | +public class CredentialsDialog extends DialogFragment { |
| 16 | + |
| 17 | + EditText usernameEditText; |
| 18 | + EditText passwordEditText; |
| 19 | + ICredentialsDialogListener listener; |
| 20 | + |
| 21 | + public interface ICredentialsDialogListener { |
| 22 | + void onDialogPositiveClick(String username, String password); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void onAttach(Context context) { |
| 27 | + super.onAttach(context); |
| 28 | + if (getActivity() instanceof ICredentialsDialogListener) { |
| 29 | + listener = (ICredentialsDialogListener) getActivity(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @NonNull |
| 34 | + @Override |
| 35 | + public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 36 | + View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_credentials, null); |
| 37 | + usernameEditText = (EditText) view.findViewById(R.id.username_edittext); |
| 38 | + passwordEditText = (EditText) view.findViewById(R.id.password_edittext); |
| 39 | + |
| 40 | + usernameEditText.setText(getArguments().getString("username")); |
| 41 | + passwordEditText.setText(getArguments().getString("password")); |
| 42 | + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) |
| 43 | + .setView(view) |
| 44 | + .setTitle("Credentials") |
| 45 | + .setNegativeButton("Cancel", null) |
| 46 | + .setPositiveButton("Continue", new DialogInterface.OnClickListener() { |
| 47 | + @Override |
| 48 | + public void onClick(DialogInterface dialog, int which) { |
| 49 | + if (listener != null) { |
| 50 | + listener.onDialogPositiveClick(usernameEditText.getText().toString(), passwordEditText.getText().toString()); |
| 51 | + } |
| 52 | + } |
| 53 | + }); |
| 54 | + return builder.create(); |
| 55 | + } |
| 56 | +} |
0 commit comments