在何处放置登录凭据检查-Android对话框 [英] Where to place sign in credentials check - android Dialog

查看:94
本文介绍了在何处放置登录凭据检查-Android对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击设置按钮时,我试图创建一个登录凭据屏幕.如果登录凭据看起来正确,则应进入设置屏幕.这是我正在关注的官方教程.

I am trying to create a login credential screen when user clicks on a setting button. And if login credentials looks right then it should to settings screen. This is the official tutorial i am following.

我设法创建一个对话框,单击该按钮后它将显示一个窗口.我还将该对话框传递回Dialog主机,这是代码段.

I am managed to create a dialog and it showing the window once the button is clicked. I also passing back the dialog to the Dialog host and here is the code snippet.

// Dialog Fragment
public class SignInDialogFragment extends AppCompatDialogFragment {
    // Use this instance of the interface to deliver action events
    private SignInDialogListener listener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.dialog_signin, null))
            .setPositiveButton("Sign in ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // Send the positive button event back to the host activity
                    listener.onDialogPositiveClick(SignInDialogFragment.this);
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    SignInDialogFragment.this.getDialog().cancel();
                }
            });

        return builder.create();
    }

    public interface SignInDialogListener {
        void onDialogPositiveClick(SignInDialogFragment dialog);
    }

    // Override the Fragment.onAttach() method to instantiate the LoginDialogListener
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            listener = (SignInDialogListener) context;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(context.toString()
                    + " must implement NoticeDialogListener");
        }
    }
}

// Main activity
public class IdleActivity extends BaseActivity implements SignInDialogFragment.SignInDialogListener {
  protected void onCreate(Bundle savedInstanceState) {
    ...
    ...
    FloatingActionButton settingsButton = findViewById(R.id.floatingButtonTools);
    buttonTools.setOnClickListener(v-> {
            showSignInDialog();
        });
   ...
   ...
  }

  private void showSignInDialog() {
    // Create an instance of the dialog fragment and show it
    SignInDialogFragment signInDialog = new SignInDialogFragment();
    signInDialog.show(getSupportFragmentManager(), "signin");
  }

  @Override
    public void onDialogPositiveClick(SignInDialogFragment dialog) {

  }
}

目前,我正在考虑对这样的凭证进行硬编码检查,稍后我将其移至服务电话.

For now i am thinking some hard coded check in credential like this and latter i will move it to a service call.

if(user.isequals("abcd") && password .isequals("1234")) {
  Intent intent = new Intent(getApplicationContext(), 
                       SettingsActivity.class);
 startActivity(intent);
}

  1. 我不确定应将此代码和凭证检查代码放在哪里?在主要活动"或登录对话框片段类"中
  2. 我看到,无论何时我单击肯定或否定按钮,它都会关闭对话框窗口并返回到主要活动,而与登录凭据的正确或错误无关.我想显示凭证对话框屏幕,直到他们输入正确的凭证为止.
  3. 我看到在运行选项卡"中有相同的打印输出,并且在对话框窗口的整个生命周期中连续不断地重复打印
  1. I am not sure where should i place this code this credential check code ? In the "main activity" or in the "Sign in dialog fragment class"
  2. I see that when ever i click on positive or negative button , its closing the dialog window and returning to the main activity irrespective of sign in credentials correct or wrong. I would like to display the credential dialog screen until they enter the correct credentials.
  3. I see that there is a same print out happening in "run tab" and its happening continuously and repeatedly throughout the life cycle of the dialog window is on the screen

推荐答案

TL; DR-默认情况下,Android Dialog Fragments在用户单击其中的任何按钮或列表选项时关闭.为避免这种情况,您需要覆盖onDismiss()方法,仅在用户输入了正确的凭据后,才继续使用默认的关闭行为.

TL;DR - Android Dialog Fragments by default close when a user clicks any button or list option in them. To prevent that, you need to override the onDismiss() method and continue with the default dismiss behaviour only if the user has entered the correct credentials.

根据有关片段的Android官方文档,

According to the official Android documentation on dialog fragments,

当用户触摸使用AlertDialog.Builder创建的任何操作按钮时,系统会为您关闭该对话框.

When the user touches any of the action buttons created with an AlertDialog.Builder, the system dismisses the dialog for you.

当用户触摸对话框列表中的项目时,系统也会关闭该对话框,除非列表使用单选按钮或复选框.否则,您可以通过在DialogFragment上调用dismiss()来手动关闭对话框.

The system also dismisses the dialog when the user touches an item in a dialog list, except when the list uses radio buttons or checkboxes. Otherwise, you can manually dismiss your dialog by calling dismiss() on your DialogFragment.

如果对话框消失时需要执行某些操作,则可以在DialogFragment中实现onDismiss()方法.

In case you need to perform certain actions when the dialog goes away, you can implement the onDismiss() method in your DialogFragment.

您也可以取消对话框.这是一个特殊事件,表明用户未完成任务就明确退出了对话框.如果用户按下后退"按钮,触摸对话框区域之外的屏幕,或者您在对话框上明确调用cancel()(例如,响应对话框中的取消"按钮),就会发生这种情况.

You can also cancel a dialog. This is a special event that indicates the user explicitly left the dialog without completing the task. This occurs if the user presses the Back button, touches the screen outside the dialog area, or if you explicitly call cancel() on the Dialog (such as in response to a "Cancel" button in the dialog).

如上面的示例所示,您可以通过在DialogFragment类中实现onCancel()来响应cancel事件.

As shown in the example above, you can respond to the cancel event by implementing onCancel() in your DialogFragment class.

因此,您必须在listener.onDialogPositiveClick()方法中检查用户的凭据是否正确.然后,您必须更新一个布尔值,例如hasUserEnteredCorrectCredentials为true.然后,您必须重写onDismiss()并仅在hasUserEnteredCorrectCredentialstrue时允许它关闭对话框,否则保持对话框打开并向用户显示错误.

So, you must check if your user's credentials are correct in the listener.onDialogPositiveClick() method. Then, you must update a boolean value, say hasUserEnteredCorrectCredentials to true. Then, you must override onDismiss() and allow it to dismiss the dialog only if hasUserEnteredCorrectCredentials is true, else keep the dialog open and show the user an error.

请记住,您将必须找到一种方法来从覆盖接口方法的位置更新布尔值hasUserEnteredCorrectCredentials.

Remember, you will have to find a way to update the boolean hasUserEnteredCorrectCredentials from the place where you override the interface method.

因此您的代码将变为-

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDialogFragment;

public class SignInDialogFragment extends AppCompatDialogFragment {

    // Use this instance of the interface to deliver action events
    private SignInDialogListener listener;
    // This boolean checks whether the credentials are correctly entered
    private boolean hasUserEnteredCorrectCredentials = false;

    // Called when the dialog fragment is created.
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Initialise an alert dialog builder
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the activity's default layout inflater to inflate the dialog fragment
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        // Set the view to the inflated fragment
        builder.setView(inflater.inflate(R.layout.dialog_signin, null))
                // Set the positive button's text and onClickListener
                .setPositiveButton("Sign in", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // Send the positive button event back to the host activity
                        // TODO - Find a way to set hasUserEnteredCorrectCredentials to true if the credentials are correct.
                        listener.onDialogPositiveClick(SignInDialogFragment.this);
                    }
                })
                // Set the positive button's text and onClickListener
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // Cancel the dialog
                        SignInDialogFragment.this.getDialog().cancel();
                    }
                });

        // Return the created dialog to the host activity
        return builder.create();
    }

    // Interface to manage clicks on the dialog's buttons
    public interface SignInDialogListener {
        // ClickListener for the positive button
        void onDialogPositiveClick(SignInDialogFragment dialog);
    }

    // Override the Fragment.onAttach() method to instantiate the SignInDialogListener
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            listener = (SignInDialogListener) context;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(context.toString()
                    + " must implement SignInDialogListener");
        }
    }

    // Called when the user clicks a button, or when the user selects an option in a list dialog
    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        // Dismiss the dialog only if the user has entered correct credentials
        if (hasUserEnteredCorrectCredentials) {
            super.onDismiss(dialog);
        }
    }
}

希望这会有所帮助!

这篇关于在何处放置登录凭据检查-Android对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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