如何管理ProgressDialog微调器[android]

时间:2016-03-20 00:09:27

标签: android progressdialog

我在ProgressDialog中遇到问题。我是初学者,我无法理解它的工作原理。这是我的第一个活动代码 -

 public class AddFriend extends Activity implements View.OnClickListener {
Button btSend, btPending;
private int progressBarStatus = 0;
private ProgressCircle progressBar = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addfriend);
    initializeVar();
    btSend.setOnClickListener(this);
    btPending.setOnClickListener(this);
}

public void initializeVar() {
    System.out.println("Initializing variables");
    btSend = (Button) findViewById(R.id.btSend);
    btPending = (Button) findViewById(R.id.btPending);
}

@Override
public void onClick(final View v) {
    progressBar = new ProgressCircle();
   // progressBar.start(v.getContext());
    System.out.println("status=" + progressBarStatus);
    progressBarStatus= click(v);
    System.out.println("status after=" + progressBarStatus);
    if (progressBarStatus == 1) {
        progressBar.stop();
    }
}

public int click(View v) {
    int status=10;
    //ProgressDialog progressDialog = ProgressDialog.show(AddFriend.this,"wait","Processin",true);
    progressBar.start(v.getContext());
    switch (v.getId()) {
        case R.id.btSend:
            Intent intent = new Intent(AddFriend.this, AddFriendSend.class);
            startActivity(intent);
            status=1;
            break;
        case R.id.btPending:
            ServerConnection sc = new ServerConnection();
            ArrayList<String> pending;
            pending = sc.pendingRequest(v);
            int size = pending.size();
            if (size == 0) {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setMessage("No Pending Request");
                alert.show();
                status=1;
            } else {
                String[] arr = new String[size];
                arr = pending.toArray(arr);
                Intent intent1 = new Intent(AddFriend.this, PendingRequest.class).putExtra("pending", arr);
                startActivity(intent1);
                status=1;
            }

            break;
    }
    while(status!=1)
    {}
    return 1;
}}

这是ProgressCircle类: -

public class ProgressCircle {
    private ProgressDialog progressBar;

    public void start(Context context) {
        progressBar = new ProgressDialog(context);
        progressBar.setIndeterminate(true);
        progressBar.setCancelable(true);
        progressBar.setMessage("Processing...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        progressBar.setProgress(0);
        progressBar.setMax(100);
        System.out.println("process Circle");
        progressBar.show();
    }

    public void stop() {
        if (progressBar != null) {
            System.out.println("process stop");
            progressBar.dismiss();

        }
    }
}

这是我的addfriend.xml: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="@drawable/background"
android:layout_height="match_parent">
<Button
    android:layout_weight="0.4"
    android:textColor="#FFFFFF"
    android:layout_centerInParent="true"
    android:background="@drawable/button_background"
    android:layout_marginTop="50dp"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:id="@+id/btSend"
    android:text="Send a new Request"
    />
<Button
    android:layout_weight="0.4"
    android:textColor="#FFFFFF"
    android:layout_centerInParent="true"
    android:background="@drawable/button_background"
    android:layout_marginTop="150dp"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:id="@+id/btPending"
    android:text="Pending Requests"/>
  </LinearLayout>

在执行上面的代码时,我没有得到任何进度对话框。虽然程序正在调用start()和stop()方法。有人请帮忙。 我只想要每当onClick()执行时,应该显示一个处理循环,直到click()方法执行完全执行。

1 个答案:

答案 0 :(得分:0)

上面的问题是你正在创建ProgressBar,但你没有将它添加到你的布局中。

将ProgressBar添加到按钮下方的布局中(并不重要)。 确保在ProgressBar上设置了一个id。

然后在onInitializeVar()中,您需要像初始化按钮一样初始化ProgressBar。