如何在登录时添加进度条?

4
我正在创建一个应用程序来登录parse.com,然后浏览项目和其他功能,但是我无法添加进度条或类似的东西,因此在应用程序登录时什么也没发生,我只是等待它登录并移动到另一个活动。
这是我登录的代码,请帮忙看看。
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import com.androidbegin.parselogintutorial.R;
    import com.parse.LogInCallback;
    import com.parse.ParseException;
    import com.parse.ParseUser;


    public class LoginActivity extends Activity {
        // Declare Variables
        Button loginbutton;
        String usernametxt;
        String passwordtxt;
        EditText password;
        EditText username;

        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Get the view from login.xml
            setContentView(R.layout.login);
            // Locate EditTexts in login.xml
            username = (EditText) findViewById(R.id.username);
            password = (EditText) findViewById(R.id.password);

            // Locate Buttons in main.xml
            loginbutton = (Button) findViewById(R.id.login);


            // Login Button Click Listener
            loginbutton.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // 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){    
                                    Intent intent = new Intent(
                                                LoginActivity.this,
                                                AddUserPage.class);
                                        startActivity(intent);
                                        Toast.makeText(getApplicationContext(),
                                                "Successfully Logged in",
                                                Toast.LENGTH_LONG).show();
                                        finish();
                                }else{
                                    Toast.makeText(getApplicationContext(), "No such user", Toast.LENGTH_LONG).show();
                                    username.setText("");
                                    password.setText("");
                                }}
                            });
                }
            });



        }
    }


我会添加一个不可见的(已隐藏)不定形(圆形)ProgressBar,并在单击按钮时将其设置为可见。然后启动新的Activity,登录Activity(包括ProgressBar)消失。 - Phantômaxx
4个回答

8

将进度条定义为private ProgressDialog mProgress;

在oncreate使用以下代码

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

现在这个。
// Login Button Click Listener
    loginbutton.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            mProgress.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){   
                            mProgress.dismiss(); 
                            Intent intent = new Intent(
                                        LoginActivity.this,
                                        AddUserPage.class);
                                startActivity(intent);
                                Toast.makeText(getApplicationContext(),
                                        "Successfully Logged in",
                                        Toast.LENGTH_LONG).show();
                                finish();
                        }else{
                            mProgress.dismiss();
                            Toast.makeText(getApplicationContext(), "No such user", Toast.LENGTH_LONG).show();
                            username.setText("");
                            password.setText("");
                        }}
                    });
        }
    });

1
请注意,以下是有关编程的内容,请勿提供代码随机:http://stackoverflow.com/questions/21636036/string-cannot-be-resolved-or-is-not-a-field // https://github.com/ejp456/Casino-Royale/blob/master/CardGames/src/com/example/cardgames/MainActivity.java - user3402040

2

将进度条添加到您的xml布局中,如下所示(确保它在所有其他视图的顶部,以便在可见时占用所有其他视图)。

<ProgressBar
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/loadingProgress"
  android:indeterminate="true"
  android:visibility="false"/>

然后在 onCreate 中获取视图引用。

ProgressBar pb =  findViewById(R.id.loadingProgress);

在“loginbutton”的公共void onClick(View arg0)中,将“pb”的可见性设置为true,如“pb.setVisibility(View.Visible)”。
在“LogInCallback()”的else部分中添加“pb.setVisiblity(View.Gone)”。

0
public class LoginActivity extends Activity {
// Declare Variables
Button loginbutton;
String usernametxt;
String passwordtxt;
EditText password;
EditText username;
private ProgressDialog mProgress;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from login.xml
    setContentView(R.layout.login);
    // Locate EditTexts in login.xml
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);


    mProgress =new ProgressDialog(this);
    String titleId="Signing in...";
    mProgress.setTitle(titleId);
    mProgress.setMessage("Please Wait...");



    // Locate Buttons in main.xml
    loginbutton = (Button) findViewById(R.id.login);



    // Login Button Click Listener
    loginbutton.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // Retrieve the text entered from the EditText
            mProgress.show();
            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){
                                mProgress.dismiss(); 
                            Intent intent = new Intent(
                                        LoginActivity.this,
                                        AddUserPage.class);
                                startActivity(intent);
                                Toast.makeText(getApplicationContext(),
                                        "Successfully Logged in",
                                        Toast.LENGTH_LONG).show();
                                finish();
                        }else{
                             mProgress.dismiss();
                            Toast.makeText(getApplicationContext(), "No such user", Toast.LENGTH_LONG).show();
                            username.setText("");
                            password.setText("");
                        }}
                    });
        }
    });



}

}


0

ProgressDialog是你想要的。它是一个模态对话框,带有一个旋转进度条,在显示时会将背景活动变成灰色。在网络调用之前启动它,并在调用完成后关闭它。

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

// do login call

然后在onDone方法中完成调用后将其关闭

pd.dismiss();

还有一些其他选项,比如ProgressBar,这是一个视图,在网络调用发生时,您将在布局中显示/隐藏它。 ActionBar 也有一个默认的 ProgressBar,如果您喜欢那种外观,可以在那里隐藏和显示。在 ActionBar 上放置 progressBar


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接