声明,初始化和调用时未显示进度对话框

时间:2017-04-13 18:23:54

标签: java android progressdialog

我正在进行活动登录,我希望它在按下登录按钮时显示进度对话框我声明,初始化并调用它但它没有显示。 但是当我在on中调用进度对话框时,它就显示出来了

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

您的所有代码都在同步运行,并且在系统有机会实际显示之前您正在关闭进度对话框。

您必须将所有登录内容放入AsyncTask或类似内容,然后才会显示进度条。虽然在你的情况下,AsyncTask会很快完成,你几乎不会注意到进度对话框。但是,如果您检查后端服务器的用户名/密码,它将与AsyncTask一起使用。

答案 1 :(得分:0)

如果您只想使用进度对话框,则应在Veriable中编写此代码。

ProgressDialog dialog;

并在Singin ClickListener中编写此代码。

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

并在您想要解雇时写下

dialog.dismiss();