2

我想在我的android應用程序中的對話框中製作進度條,以便當用戶按下按鈕對話框時出現文本如「Please wait」和移動進度條。我不知道該怎麼做。在Android中的對話框中的進度條

下面是代碼:

package com.example.progressdialog; 

import android.os.Bundle; 
import android.view.View; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 


    ProgressDialog pd = new ProgressDialog(this); 

    /** Called when the activity is first created. */ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     pleasewait(); 
    } 



    public void pleasewait(View v){ 

    pd.setTitle(「Hello」); 


      pd.show(); 
    } 

} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Please wait…" 
     android:onClick=」pleasewait」 /> 

</LinearLayout> 

請在我的代碼更正或,如果可以的話,提出一些更好的辦法。

當我試圖建議

enter image description here

回答

1

你可以試一下這個:

public class MainActivity extends Activity { 
Button button; 
ProgressDialog progressDoalog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button = (Button) findViewById(R.id.button1); 
    button.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     progressDoalog = new ProgressDialog(MainActivity.this); 
     progressDoalog.setMax(100); 
     progressDoalog.setMessage("Its loading...."); 
     progressDoalog.setTitle("ProgressDialog bar example"); 
     progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progressDoalog.show(); 
     new Thread(new Runnable() { 
     @Override 
     public void run() { 
         try { 
    while (progressDoalog.getProgress() <= progressDoalog 
    .getMax()) { 
     Thread.sleep(200); 
     handle.sendMessage(handle.obtainMessage()); 
     if (progressDoalog.getProgress() == progressDoalog 
    .getMax()) { 
     progressDoalog.dismiss(); 
     } 
    } 
      } catch (Exception e) { 
    e.printStackTrace(); 
    } 
     } 
     }).start(); 
    } 

    Handler handle = new Handler() { 
    @Override 
     public void handleMessage(Message msg) { 
     super.handleMessage(msg); 
     progressDoalog.incrementProgressBy(1); 
     } 
    }; 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items 
       //to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

} 

EXAMPLE

在短:

Example: 

import android.app.ProgressDialog; 
//in the class... 
private ProgressDialog progressBar; 

//when you want the dialog to show the first time. 
progressBar = ProgressDialog.show(Main.this, "Disconnecting", "Please wait for few secs..."); 

//when you want the progressbar to disappear 
if (progressBar.isShowing()) { 
progressBar.dismiss(); 
} 
+0

I am g使用此代碼設置太多錯誤... :( – LittleGirl 2014-09-18 23:22:51

+0

是的,當然!:)可能是我缺少導入的東西。 – LittleGirl 2014-09-18 23:31:33

+0

24錯誤.... :( – LittleGirl 2014-09-18 23:37:36

1

你需要的是申報進度對話框

ProgressDialog progress;

顯示它機智的數據,如這裏

progress = ProgressDialog.show(this, "dialog title","dialog message", true);

,比關閉它,只要你有

progress.dismiss();