登录尝试与持续时间 [英] Login Attempt with duration

查看:210
本文介绍了登录尝试与持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的登录尝试,但问题是我想有时间记录。我的意思是,如果尝试走了,我想提出一个额外的信息说:你试图达到0,请等待3分钟,再次登录或者类似的东西。

I did the login attempt but the problem is I want to have duration to log. I mean, if the attempt is gone, I want to put an extra information saying "Your attempt reach 0, please wait 3 minutes to log again" or something like that.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"  tools:context=".Mobile_Grocery"
android:id="@+id/mobile_grocery"
>

<ImageView
    android:id="@+id/mobile_grocery_bckgrnd"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/mobile_grocery"
    android:scaleType="centerCrop"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/mobile_grocery_main"
    android:id="@+id/mobilegrocery"
    android:layout_marginTop="40dp"
    android:textSize="50dp"
    android:textColor="#000000"
    android:textStyle="bold|italic"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/Email"
    android:hint="@string/email_text"
    android:textColorHint="#000000"
    android:layout_above="@+id/Password"
    android:layout_alignLeft="@+id/Password"
    android:layout_alignStart="@+id/Password" />

<EditText
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:id="@+id/Password"
    android:hint="@string/password_text"
    android:textColorHint="#000000"
    android:layout_marginBottom="119dp"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/mobilegrocery"
    android:layout_alignStart="@+id/mobilegrocery"
    android:layout_marginLeft="37dp"
    android:layout_marginStart="37dp"
    android:password="true" />

<ImageButton
    android:layout_width="100dp"
    android:layout_height="45dp"
    android:id="@+id/Login"
    android:layout_marginTop="43dp"
    android:layout_below="@+id/Email"
    android:layout_alignLeft="@+id/Password"
    android:layout_alignStart="@+id/Password"
    android:src="@drawable/login_button"
    android:scaleType="centerCrop"
    android:layout_marginLeft="48dp"
    android:layout_marginStart="48dp"
    android:onClick="ocLogin"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/attempt_text"
    android:id="@+id/attleft"
    android:textSize="20dp"
    android:textColor="#000000"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ImageButton
    android:layout_width="100dp"
    android:layout_height="45dp"
    android:id="@+id/register"
    android:src="@drawable/register"
    android:scaleType="centerCrop"
    android:layout_below="@+id/Login"
    android:layout_alignLeft="@+id/Login"
    android:layout_alignStart="@+id/Login" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/number"
    android:textColor="#000000"
    android:textSize="20dp"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/register"
    android:layout_alignStart="@+id/register"
    android:password="false" />

public class Mobile_Grocery extends ActionBarActivity {
private static EditText email;
private static EditText password;
private static TextView attempts;
private static ImageButton login_btn;
int attempt_counter = 3 ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mobile__grocery);
    ocLogin();
}

public void ocLogin () {
    email = (EditText) findViewById(R.id.Email);
    password = (EditText) findViewById(R.id.Password);
    attempts = (TextView) findViewById(R.id.number);
    login_btn = (ImageButton) findViewById(R.id.Login);

    attempts.setText(Integer.toString(attempt_counter));

    login_btn.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (email.getText().toString().equals("admin") &&
                            password.getText().toString().equals("pass")) {
                        Toast.makeText(Mobile_Grocery.this, "Email and Password is correct",
                                Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent("com.example.admin.mobile_grocery.Method");
                        startActivity(intent);
                    } else {
                        Toast.makeText(Mobile_Grocery.this, "Email and Password is incorrect",
                                Toast.LENGTH_SHORT).show();
                        attempt_counter--;
                        attempts.setText(Integer.toString(attempt_counter));
                        if (attempt_counter == 0) {
                            login_btn.setEnabled(false);
                        }
                    }

                }
            }
    );

}

我怎么能这样做?

How can I do that?

推荐答案

您可以做这样的事情,

首先定义

private static final int WAIT_TIME = 3 * 60 * 1000;

private int loginAttempts = 3;

在您的登录按钮的点击监听器,

In your login button click listener,

    login_btn.setOnClickListener(
                new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(loginAttempts == 0) {

                    Toast.makeText(Mobile_Grocery.this, "Your attempt reach 0, please wait 3 minutes to log again", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (email.getText().toString().equals("admin") &&
                        password.getText().toString().equals("pass")) {
                    Toast.makeText(Mobile_Grocery.this, "Email and Password is correct",
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent("com.example.admin.mobile_grocery.Method");
                    startActivity(intent);

                } else {

                    loginAttempts--;

                    Toast.makeText(Mobile_Grocery.this, "Email and Password is incorrect",
                            Toast.LENGTH_SHORT).show();

                    if(loginAttempts == 2) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            loginAttempts = 3;
                        }
                    }, WAIT_TIME);
                   }


                }

            }
        }
        );

这篇关于登录尝试与持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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