显示针对按钮的循环进度条单击使用计时器类

时间:2013-06-28 21:09:59

标签: java android class timer progress-bar

我用两个输入框和一个总按钮编写了简单的计算器,我需要的是显示circular Progressbar for 3 seconds before i show result after pressing Total button,这就是我所做的。

package com.example.calculator;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity 
   {
     EditText t1;
     EditText t2;
     TextView tv;
     TextView tv1; 
     Timer singleTask;
     int Interval = 3000; 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1= (EditText)findViewById(R.id.editText1);
        t2= (EditText)findViewById(R.id.editText2);
        tv=  (TextView)findViewById(R.id.textView1);
        tv1= (TextView)findViewById(R.id.textView2);
        singleTask= new Timer();

    }
   public void loadprogressbar()
    {
          singleTask.schedule(new TimerTask() {
                @Override
                public void run() {

                // have to add code for progress bar but right now just caption text added 
                    tv1.setText("This is for 3 seconds only"); 

                }
                }, 1000);

    }

   @Override
   protected void onDestroy(){
   super.onDestroy();
   if(singleTask != null)
   {
   singleTask.cancel();
   }
   }
   public void   Clicked(View v)
    {    int  total; 


         if(v.getId()==R.id.button1)
         {     
             int v1 = Integer.parseInt(t1.getText().toString());
             int v2 = Integer.parseInt(t2.getText().toString());
             total = v1 + v2;
             loadprogressbar();
             tv.setText(total+"");
             tv.setVisibility(1);
         }
         else if (v.getId()==R.id.button2)
         {
             t1.setText("");
             t2.setText("");
         }
    }


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

}

Xml文件是

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:visibility="invisible"/>
     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        ></TextView>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/editText2"
        android:text="Clear"
        android:onClick="Clicked" 
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="35dp"
        android:text="Total"
        android:onClick="Clicked"
         />

</RelativeLayout>

问题是我没有得到任何针对定时器代码的结果?根据我的想法它应该显示文本3秒然后显示文本。我的逻辑也可能是错的,请指导我..

3 个答案:

答案 0 :(得分:3)

如果我理解你想要做的是显示一个ProgressDialog来模拟你的计算器正在做一些计算。 Timer类不是您想要使用的。基本上,您希望等待3秒(在此期间可以看到ProgressDialog),然后通过设置带有结果的TextView来更新UI。您可能知道,您无法直接从后台线程修改UI线程,也不应该让UI线程休眠。因此,AsyncTask是您最好的选择。下面是一个示例代码,允许您显示ProgressDialog 3秒钟。您应该在OnPostExecute方法上更新TextView,因为它在UI线程上运行。

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity{
ProgressDialog pd;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      pd = new ProgressDialog(this);
    pd.setTitle(getString("Title"));
    pd.setIndeterminate(true);
   pd.show();
    new MyAsycnTask().execute();

    }
}


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

private class MyAsycnTask extends AsyncTask <Void,Void,Void> {


    @Override
    protected Void doInBackground(Void... voids) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;

    }


    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        pd.hide();




    }
}
}

答案 1 :(得分:2)

与E.Odebugg一起回答。

如果您需要显示TextView 3秒然后隐藏,请按以下步骤操作,不使用TimerTask

textView.postDelayed(new Runnable() {
    @Override
    public void run() {
        textView.setVisibility(View.GONE);
    }
}, 3000);

默认显示TextView,3秒后隐藏。另请注意,使用了postDelayed(),它将在UI线程上运行Runnable

答案 2 :(得分:1)

所以我认为你的代码在loadprogressbar()中是错误的。它应该是:

public void loadprogressbar()
{
      // have to add code for progress bar but right now just caption text added 
      tv1.setText("This is for 3 seconds only"); 
      singleTask.schedule(new TimerTask() {
            @Override
            public void run() {

            runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                                 ///hide the progressbar here...
                              }
                            });

            }
            }, Interval);

}

如果我是你,我会更改你的功能名称“Clicked”,因为它可以是一个保留字(颜色也表示它)......