gpt4 book ai didi

java - 声明、初始化和调用时不显示进度对话框

转载 作者:行者123 更新时间:2023-12-02 13:25:34 31 4
gpt4 key购买 nike

我正在创建一个登录 Activity ,我希望它在按下登录按钮时显示进度对话框,我声明、初始化并调用了它,但它没有显示。但是当我在创建时调用进度对话框时,它出现了

这是我的代码:

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

最佳答案

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

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

关于java - 声明、初始化和调用时不显示进度对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43399247/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com