2017-04-13 40 views
0

我正在做一個登錄活動,我希望它顯示進度對話框時,按下登錄按鈕我宣佈,初始化,並調用它,但它不顯示。 但是當我打電話的進展中的對話框上創建它出現了進度對話框聲明,初始化和調用時不顯示

這是我下面的代碼:

public class Login extends Activity { 
private EditText username, password; 
private Button login; 
private Button signup; 
String txtUsername, txtPassword, loggedin; 
private ProgressDialog mProgress; 
String titleId = "Logging in"; 
int id; 

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

    final DatabaseHelper db = new DatabaseHelper(this); 

    username = (EditText) findViewById(R.id.username); 
    password = (EditText) findViewById(R.id.password); 
    login = (Button) findViewById(R.id.login); 
    signup = (Button) findViewById(R.id.signup); 

    // Login button listener 
    login.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      showProgress(); 
      // Retrieve text from the EditText 
      txtUsername = username.getText().toString(); 
      txtPassword = password.getText().toString(); 

      if (txtUsername.isEmpty() && txtPassword.isEmpty()) { 
       mProgress.dismiss(); 
       Toast.makeText(getApplicationContext(),"Enter Username And Password", Toast.LENGTH_LONG); 

      } else { 
       User user = db.getUser(txtUsername, txtPassword); 
       id = user.getId(); 

       if (txtUsername.equalsIgnoreCase(user.getUsername()) 
         && txtPassword.equals(user.getPassword())) { 
        mProgress.dismiss(); 
        Intent intent = new Intent(Login.this, 
          AdministratorPage.class); 
        intent.putExtra("username", "username"); 
        intent.putExtra("id", "id"); 
        startActivity(intent); 
        Toast.makeText(
          getApplicationContext(), 
          "Successfully Logged In as " 
            + user.getUsername(), Toast.LENGTH_LONG) 
          .show(); 
        finish(); 
       } else { 
        mProgress.dismiss(); 
        Toast.makeText(getApplicationContext(), 
          "Usename Or Password Incorrect", 
          Toast.LENGTH_LONG).show(); 
        username.setText(""); 
        password.setText(""); 
       } 
      } 
     } 

    }); 

    // Sign up button listener 
    signup.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      showProgress(); 
      // Retrieve text from the EditText 
      txtUsername = username.getText().toString(); 
      txtPassword = password.getText().toString(); 

      if (txtUsername.isEmpty() && txtPassword.isEmpty()) { 
       mProgress.dismiss(); 
       Toast.makeText(getApplicationContext(), 
         "Enter Username And Password", Toast.LENGTH_LONG); 

      } else { 

       // Inserting Contacts 
       Log.d("Insert: ", "Inserting .."); 
       db.addUser(new User(txtUsername, txtPassword, "Y")); 

       // Reading all contacts 
       Log.d("Reading: ", "Reading all contacts.."); 
       List<User> users = db.getAllUsers(); 

       for (User cn : users) { 
        String log = "Id: " + cn.getId() + " ,Name: " 
          + cn.getUsername() + " ,Password: " 
          + cn.getPassword(); 
        // Writing Contacts to log 
        Log.d("Name: ", log); 
       } 
       mProgress.dismiss(); 
       Intent intent = new Intent(Login.this, 
         AdministratorPage.class); 
       startActivity(intent); 
       Toast.makeText(getApplicationContext(), 
         "Successfully Logged In", Toast.LENGTH_LONG).show(); 
       finish(); 
      } 
     } 

    }); 

} 

public void showProgress() { 
    // Initialize the Progress Dialog 
    mProgress = new ProgressDialog(Login.this); 
    mProgress.setTitle(titleId); 
    mProgress.setMessage("Logging In Please Wait..."); 

    mProgress.show(); 
} 
+0

我不知道你在期待什麼,但在你的if/else結構,你是關閉該對話框.....所以我猜這個對話框就可以看出一些毫秒,但被駁回直接.. – Opiatefuchs

+0

我每一個案件'正在模擬器上測試它,所以通常會顯示出來,而且我最近編寫了一個類似的活動,它使用了相同的方法來顯示進度對話框,並且它正在顯示但不是此活動 –

回答

2

如果你想簡單的使用進度對話框你應該寫在Veriable此代碼。

ProgressDialog dialog; 

and in Singin ClickListener編寫此代碼。

dialog = ProgressDialog.show(Login.this, "Logging In", "Please wait...", true); 

,當你想放棄

dialog.dismiss(); 
1

所有代碼都同步運行但駁回進度對話框中的系統有機會真正表現出來之前寫這篇文章。

您必須將所有登錄內容放入AsyncTask或類似內容中,纔會顯示進度條。雖然在你的情況下,AsyncTask會很快完成,你很難注意到進度對話框。但是,如果你要在後臺服務器上檢查用戶名/密碼,它將與AsyncTask一起工作。