登录时如何进行进度条

时间:2017-08-16 07:51:45

标签: java android

我想在登录成功时创建进度条,但是当我尝试它时。它崩溃或者我的代码错了。请帮我修理一下。而且我不知道在哪里将进度条放在xml中。或者我必须为进度条制作新的布局?

这是我的activity.java

public class MainActivity extends Activity {

private AlertDialog.Builder alertDialogBuilder;
Button btnSignIn,btnSignUp;
LoginDataBaseAdapter loginDataBaseAdapter;
ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {

    alertDialogBuilder = new  AlertDialog.Builder(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // create a instance of SQLite Database
    loginDataBaseAdapter=new LoginDataBaseAdapter(this);
    loginDataBaseAdapter=loginDataBaseAdapter.open();

    // Get The Refference Of Buttons
    btnSignIn=(Button)findViewById(R.id.buttonSignIN);
    btnSignUp=(Button)findViewById(R.id.buttonSignUP);

    // Set OnClick Listener on SignUp button
    btnSignUp.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  abd Start The Activity
            Intent intentSignUP=new Intent(getApplicationContext(),SignUPActivity.class);
            startActivity(intentSignUP);
        }
    });
}
// Methos to handleClick Event of Sign In Button
public void signIn(View V)
{
    final Dialog dialog = new Dialog(MainActivity.this);
    dialog.setContentView(login);
    dialog.setTitle("Login");

    // get the Refferences of views
    final  EditText editTextUserName=(EditText)dialog.findViewById(R.id.username);
    final  EditText editTextPassword=(EditText)dialog.findViewById(R.id.password);
    progressBar.setVisibility(View.GONE);

    Button btnSignIn=(Button)dialog.findViewById(R.id.sumbit);

    // Set On ClickListener
    btnSignIn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // get The User name and Password
            String userName=editTextUserName.getText().toString();
            String password=editTextPassword.getText().toString();

            // fetch the Password form database for respective user name
            String storedPassword=loginDataBaseAdapter.getSinlgeEntry(userName);

            // check if the Stored password matches with  Password entered by user
            if(password.equals(storedPassword))
            {

                Intent n = new Intent(MainActivity.this, MenuAwal.class);
                n.putExtra("username", userName);
                startActivity(n);

                Toast.makeText(MainActivity.this, "Congrats: Login Successfull", Toast.LENGTH_LONG).show();
                dialog.dismiss();

                progressBar.setVisibility(View.VISIBLE);

            }
            else
            {
                Toast.makeText(MainActivity.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
            }
        }
    });

    dialog.show();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    // Close The Database
    loginDataBaseAdapter.close();
}

@Override
public void onBackPressed() {

    alertDialogBuilder.setTitle("Tutup Aplikasi");
    alertDialogBuilder.setMessage("Apakah Anda yakin ingin menutup aplikasi?")
            .setCancelable(false).setPositiveButton("YA", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            finish();
        }
    })
            .setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            }).create().show();
}
}

3 个答案:

答案 0 :(得分:0)

定义

private ProgressDialog myProgress;

onCreate使用

myProgress = new ProgressDialog(context);
myProgress.setTitle("Processing...");
myProgress.setMessage("Please wait...");
myProgress.setCancelable(false);
myProgress.setIndeterminate(true);

之后,将其实现到您的代码中:

   loginbutton.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        myProgress.show();
        // Retrieve the text entered from the EditText
        usernametxt = username.getText().toString();
        passwordtxt = password.getText().toString();

        // Send data to Parse.com for verification
        ParseUser.logInInBackground(usernametxt, passwordtxt,
                new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                            // If user exist and authenticated, send user to Welcome.class
                        if(user !=null){   
                        myProgress.dismiss(); 
                        Intent intent = new Intent(
                                    LoginActivity.this,
                                    AddUserPage.class);
                            startActivity(intent);
                            Toast.makeText(getApplicationContext(),
                                    "Successfully Logged in",
                                    Toast.LENGTH_LONG).show();
                            finish();
                    }else{
                        myProgress.dismiss();
                        Toast.makeText(getApplicationContext(), "No such user", Toast.LENGTH_LONG).show();
                        username.setText("");
                        password.setText("");
                    }}
                });
    }
});

答案 1 :(得分:0)

创建一个类 BaseActivity.java ,类似于

public class BaseActivity extends AppCompatActivity{
 public ProgressDialog prsDlg;

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prsDlg = new ProgressDialog(this);
}

public void showProgressDialog() {

        prsDlg.setMessage("Please wait...");
        prsDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        prsDlg.setIndeterminate(true);
        prsDlg.setCancelable(false);
        prsDlg.show();
    }

    public void dismissProgressDialog() {
        if(prsDlg!=null){
            if (prsDlg.isShowing()) {
                prsDlg.dismiss();
            }
        }

    }
}

然后将此BaseActivity类扩展到MainActivity类:

public class MainActivity extends BaseActivity {

现在使用您的Activity中的 showProgressDialog() dismissProgressDialog()方法。它将允许您从所有活动中重复使用。
喜欢,

private class LoginAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            //your code
        }

        @Override
        protected void onPreExecute() {
          showProgressDialog();
        }

        @Override
        protected void onPostExecute(String result) {
          dismissProgressDialog();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }

答案 2 :(得分:0)

如果您在xml中定义了进度条,那么请在onCreate中定义它。我只是在您的类中看到Progress的声明,但没有任何id的引用。我想这就是为什么你会崩溃。