在android中单击按钮时显示带有消息的进度条

时间:2011-04-01 09:42:51

标签: android progress-bar

我想显示进度条,当我点击登录按钮时,消息登录成功我怎么能在android中做任何身体指导我如果可能的话请给出代码

1 个答案:

答案 0 :(得分:1)

了解您需要做的是:当用户点击按钮时,启动后台服务(此处验证用户身份)。在进行后台处理时,您必须显示进度条。根据结果​​显示相应的消息,无论用户是否被授权,例如在Toast或其他活动中说。

要进行冗长的操作,必须使用Thread或AsyncTask。它同时处理UI和后台进程。

你要做的是:在onClick事件中调用AsyncTask。在这里,你必须创建一个LoginOperation对象,然后只需调用它的LoginOperation.execute()方法。

为了显示结果,您可以像以下一样添加消息:

Toast.makeText(context,"Your Message",Toast.LENGTH_LONG).show();

当然,你必须在AsyncTask的onPostExecute()中添加它。

private class LoginOperation extends AsyncTask<String, Void, String> {

private ProgressDialog Dialog = new ProgressDialog(ClassName.this);

@Override
protected String doInBackground(String... params) {
// perform long running operation operation
//Here you have to do your network operations..
return null;
}


/* (non-Javadoc)
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/

@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation

try
     {
          if(Dialog.isShowing())
          {
              Dialog.dismiss();
          }
          // do your Display and data setting operation here
     }
     catch(Exception e)
     {

     }
//Here depending upon your validation, display appropriate message. 
If(correct_user)
call another activity
else
display error toast message in the same activity


}

/* (non-Javadoc)
* @see android.os.AsyncTask#onPreExecute()
*/

@Override

protected void onPreExecute() {
    Dialog.setMessage("Authenticating.....");
    Dialog.show();

// Things to be done before execution of long running operation. For example showing ProgessDialog

}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onProgressUpdate(Progress[])
*/

@Override
protected void onProgressUpdate(Void... values) {
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}

}