2012-06-11 44 views
6

Tôi có một hộp thoại cảnh báo trong ứng dụng của tôi để xác thực đăng nhập. Khi gửi yêu cầu tôi muốn hiển thị một thanh tiến trình và muốn bỏ nếu câu trả lời là success.Please giúp tôi nếu có ai knows.Iam sử dụng mã dưới đây:
hiển thị thanh tiến trình trong hộp thoại cảnh báo

final AlertDialog.Builder alert = new AlertDialog.Builder(this); 
LinearLayout login = new LinearLayout(this); 
TextView tvUserName = new TextView(this); 
TextView tvPassword = new TextView(this); 
TextView tvURL = new TextView(this); 
final EditText etUserName = new EditText(this); 
final EditText etPassword = new EditText(this); 
final EditText etURL = new EditText(this); 
login.setOrientation(1); // 1 is for vertical orientation 
tvUserName.setText(getResources().getString(R.string.username)); 
tvPassword.setText(getResources().getString(R.string.password)); 
tvURL.setText("SiteURL"); 
login.addView(tvURL); 
login.addView(etURL); 
login.addView(tvUserName); 
login.addView(etUserName); 
login.addView(tvPassword); 
etPassword.setInputType(InputType.TYPE_CLASS_TEXT 
| InputType.TYPE_TEXT_VARIATION_PASSWORD); 
login.addView(etPassword); 
alert.setView(login); 
alert.setTitle(getResources().getString(R.string.login)); 
alert.setCancelable(true); 
alert.setPositiveButton(getResources().getString(R.string.login), 
new DialogInterface.OnClickListener() { 
    public void onClick(final DialogInterface dialog, 
    int whichButton) { 
     strhwdXml = etURL.getText().toString(); 
     strUserName = etUserName.getText().toString(); 
     XmlUtil.username = strUserName; 
     strPassword = etPassword.getText().toString(); 
     if ((strUserName.length() == 0) 
     && (strPassword.length() == 0) 
     && (strhwdXml.length() == 0)) { 
      Toast.makeText(
      getBaseContext(), 
      getResources().getString(
      R.string.userPassword), 
      Toast.LENGTH_SHORT).show(); 
      onStart(); 
      } else { 
      final SharedPreferences prefs = PreferenceManager 
      .getDefaultSharedPreferences(getApplicationContext()); 
      SharedPreferences.Editor prefsEditor = prefs 
      .edit(); 
      try { 
       StringBuffer inStreamBuf = new StringBuffer(); 
       inStreamBuf = XmlUtil 
       .getLoginAuthResponse(strUserName, 
       strPassword, strhwdXml); 
       strXmlResponse = inStreamBuf.toString(); 
       Log.e("Response:", strXmlResponse); 
       String parsedXML = ParseResponse(strXmlResponse); 
       if (parsedXML 
       .equalsIgnoreCase(getResources() 
       .getString(R.string.success))) { 
       } 
+0

Sử dụng Tác vụ không đồng bộ cho điều này, http://droidapp.co.uk/?p=177 – Aerrow

+0

có thể dễ dàng hơn nếu bạn định nghĩa bố cục của mình theo xml – jiduvah

+0

cách tôi có thể thực hiện việc này ... vui lòng giúp tôi jiduvah –

Trả lời

24

Nó có thể được dễ dàng hơn để sử dụng

này
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
         "Loading. Please wait...", true); 

Bạn có thể đọc thêm về các hộp thoại tiến here

Để hủy sẽ

dialog.dismiss(); 
+0

cách hủy nếu phản hồi xác thực thành công?!? –

+0

i được cập nhật cho bạn – jiduvah

+0

sẽ mở một hộp thoại mới, phải không? Đó có phải là hiệu ứng mong muốn không? Bạn cũng có thể thêm progressdialog vào hộp thoại chính nó – MikeIsrael

1

Hãy thử mã dưới đây

private class DownloadingProgressTask extends 
     AsyncTask<String, Void, Boolean> { 

    private ProgressDialog dialog = new ProgressDialog(ShowDescription.this); 

    /** progress dialog to show user that the backup is processing. */ 

    /** application context. */ 

    protected void onPreExecute() { 
     this.dialog.setMessage("Please wait"); 
     this.dialog.show(); 
    } 

    protected Boolean doInBackground(final String... args) { 
     try { 
      // write your request code here 


      **StringBuffer inStreamBuf = new StringBuffer(); 
      inStreamBuf = XmlUtil 
      .getLoginAuthResponse(strUserName, 
      strPassword, strhwdXml); 
      strXmlResponse = inStreamBuf.toString(); 
      Log.e("Response:", strXmlResponse); 
      String parsedXML = ParseResponse(strXmlResponse); 
      if (parsedXML 
      .equalsIgnoreCase(getResources() 
      .getString(R.string.success))) {** 

       return true; 
     } catch (Exception e) { 
      Log.e("tag", "error", e); 
      return false; 
     } 
    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 

     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 

     if (success) { 
      Toast.makeText(ShowDescription.this, 
        "File successfully downloaded", Toast.LENGTH_LONG) 
        .show(); 
      imgDownload.setVisibility(8); 
     } else { 
      Toast.makeText(ShowDescription.this, "Error", Toast.LENGTH_LONG) 
        .show(); 
     } 
    } 

} 

và gọi đây là trong onclick sự kiện

new DownloadingProgressTask().execute(); 
+0

u có thể chỉnh sửa mã của tôi và cho tôi biết làm thế nào để thực hiện nó trong ứng dụng của tôi –

+0

tôi đã chỉnh sửa mã. vui lòng kiểm tra – Nirali

4

Nếu bạn muốn thanh tiến trình để hiển thị, hãy thử các bước sau đây và bạn cũng có thể sao chép và dán toàn bộ mã phần có liên quan của mã của bạn và nó sẽ hoạt động.

//the first thing you need to to is to initialize the progressDialog Class like this 

final ProgressDialog progressBarDialog= new ProgressDialog(this); 

//set the icon, title and progress style.. 

progressBarDialog.setIcon(R.drawable.ic_launcher); 

progressBarDialog.setTitle("Showing progress..."); 

progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 


    //setting the OK Button 
    progressBarDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, 
       int whichButton){ 
      Toast.makeText(getBaseContext(), 
        "OK clicked!", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    //set the Cancel button 
    progressBarDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int whichButton){ 
      Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
//initialize the dialog.. 
progressBarDialog.setProgress(0); 

//setup a thread for long running processes 
new Thread(new Runnable(){ 
    public void run(){ 
     for (int i=0; i<=15; i++){ 
      try{ 
       Thread.sleep(1000); 
       progressBarDialog.incrementProgressBy((int)(5)); 
      } 
      catch(InterruptedException e){ 
       e.printStackTrace(); 
      } 
     } 
     //dismiss the dialog 
     progressBarDialog.dismiss(); 
    } 
    }); 

//show the dialog 
progressBarDialog.show(); 

Nút hủy sẽ bỏ qua hộp thoại.

+0

Chỉ cần thêm .start() sau khi tạo chuỗi. – jelic98

Các vấn đề liên quan