简体   繁体   English

持续时间登录尝试

[英]Login Attempt with duration

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.我的意思是,如果尝试消失了,我想添加一个额外的信息,说“您的尝试达到 0,请等待 3 分钟再次登录”或类似的内容。

Login Form XML登录表单 XML

<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" />

Login Java登录Java

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?我怎样才能做到这一点?

You can do something like this,你可以做这样的事情,

First define首先定义

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);
                   }


                }

            }
        }
        );

I recommend as following.我推荐如下。

login(){

    //show your loading text visible
    new Thread(new Runnable() {

        @Override
        public void run() {
            boolean loginTrue = //request your web login method                 

            //You don'y need handler to pause thread. Http request will pause until response from server                

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    //set invisible your laoding text
                    if(loginTrue){
                        Toast.makeText(Mobile_Grocery.this, "Email and Password is correct",
                            Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(Mobile_Grocery.this, "Email and Password is incorrect",
                            Toast.LENGTH_SHORT).show();
                    }

                }
            });


        }
    }).start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM