2017-03-27 34 views
0

我正在學習從YouTube上製作一個簡單的登錄應用程序,但是,當我構建代碼時,儘管應用程序運行良好,但它從未將值3分配給嘗試計數器代碼,只有佈局是可見的,因此登錄不起作用。你能幫我麼?如果您需要其他文件,請將其寫入評論中,稍後再上傳。謝謝。運行簡單登錄應用程序的問題

package com.shubham.splashscreenemulation; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 


public class UserLogin extends AppCompatActivity { 

private static EditText username; 
private static EditText password; 
private static TextView attempts_left; 
private static Button login_btn; 
int attempt_counter = 3; 


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

public void LoginButton(){ 

    username = (EditText)findViewById(R.id.editText_user); 
    password = (EditText)findViewById(R.id.editText_password); 
    attempts_left = (TextView)findViewById(R.id.textView_attempt_count); 
    login_btn = (Button)findViewById(R.id.button_login); 

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

    login_btn.setOnClickListener(
      new View.OnClickListener(){ 

       @Override 
       public void onClick(View v){ 

        if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev")) 
        { 
         Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show(); 
         Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen"); 
         startActivity(intent); 
        } 

        else 
        { 
         Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show(); 
         attempt_counter--; 
         attempts_left.setText(Integer.toString(attempt_counter)); 
         if(attempt_counter == 0) 
         { 
          login_btn.setEnabled(false); 
         } 
        } 
       } 


      } 

    ); 
} 


} 

回答

0

您不要在Activity中調用函數LoginButton()。

這裏有一些提示:函數名以小寫字母開頭,如loginButton(帶有camelCase),類名以像AppCompatActivity這樣的大寫字母開頭。這個提示是爲了更好的代碼可讀性。爲什麼你在該函數中聲明Views是靜態的?

package com.shubham.splashscreenemulation; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 


public class UserLogin extends AppCompatActivity { 

private static EditText username; 
private static EditText password; 
private static TextView attempts_left; 
private static Button login_btn; 
int attempt_counter = 3; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_user_login); 
    LoginButton(); 

} 

public void LoginButton(){ 

    username = (EditText)findViewById(R.id.editText_user); 
    password = (EditText)findViewById(R.id.editText_password); 
    attempts_left = (TextView)findViewById(R.id.textView_attempt_count); 
    login_btn = (Button)findViewById(R.id.button_login); 

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

    login_btn.setOnClickListener(
      new View.OnClickListener(){ 

       @Override 
       public void onClick(View v){ 

        if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev")) 
        { 
         Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show(); 
         Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen"); 
         startActivity(intent); 
        } 

        else 
        { 
         Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show(); 
         attempt_counter--; 
         attempts_left.setText(Integer.toString(attempt_counter)); 
         if(attempt_counter == 0) 
         { 
          login_btn.setEnabled(false); 
         } 
        } 
       } 


      } 

    ); 
} 
+0

u能闡述 – ShubhGOYAL5

+0

這是你的第一個Android項目?如果是這樣,你應該遵循一些課程(例如Udacity,是免費的和好的),這會幫助你進入它。 – sgrumo

+0

非常感謝.. – ShubhGOYAL5

0

您可以setContentView之後添加LoginButton()或刪除LoginButton方法,將裏面的onCreate一切。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_user_login); 
     username = (EditText)findViewById(R.id.editText_user); 
     password = (EditText)findViewById(R.id.editText_password); 
     attempts_left = (TextView)findViewById(R.id.textView_attempt_count); 
     login_btn = (Button)findViewById(R.id.button_login); 
     attempts_left.setText(Integer.toString(attempt_counter)); 
     login_btn.setOnClickListener(new View.OnClickListener(){ 

      @Override 
       public void onClick(View v){ 
       if(username.getText().toString().equals("Shubh") && password.getText().toString().equals("adidev")) 
        { 
        Toast.makeText(UserLogin.this, "Login credentials are correct!", Toast.LENGTH_SHORT).show(); 
        Intent intent = new Intent("com.shubham.splashscreenemulation.SplashScreen"); 
        startActivity(intent); 
        } 
        else 
        { 
         Toast.makeText(UserLogin.this, "Login credentials are not correct!", Toast.LENGTH_SHORT).show(); 
         attempt_counter--;      attempts_left.setText(Integer.toString(attempt_counter)); 
         if(attempt_counter == 0) 
         { 
          login_btn.setEnabled(false); 
         } 
        } 
       } 
       }); 
      } 

你也可以改變你的setText這個attempts_left.setText(attempt_counter+"");

+0

沒有工作... – ShubhGOYAL5

+0

再次檢查.... –

+1

非常感謝你,但我可以問爲什麼它不這樣工作? – ShubhGOYAL5