登录尝试的持续时间

时间:2015-10-11 09:03:24

标签: java android xml android-layout android-studio

我进行了登录尝试,但问题是我想要记录持续时间。我的意思是,如果尝试消失了,我想提供一个额外的信息说"你的尝试达到0,请等待3分钟再次登录"或类似的东西。

登录表单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" />

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

                }
            }
    );

}

我该怎么做?

2 个答案:

答案 0 :(得分:1)

你可以这样做,

首先定义

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

private int loginAttempts = 3;

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

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


                }

            }
        }
        );

答案 1 :(得分:0)

我建议如下。

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