【问题标题】:How to manage ProgressDialog spinner [android]如何管理 ProgressDialog 微调器 [android]
【发布时间】:2016-03-20 08:32:27
【问题描述】:

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

 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() 方法中的代码完全执行。

【问题讨论】:

    标签: android progressdialog


    【解决方案1】:

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

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

    然后在 onInitializeVar() 中,您需要以与初始化按钮相同的方式初始化 ProgressBar。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 2014-03-08
      • 2016-04-11
      相关资源
      最近更新 更多