0

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

包 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 秒,然后显示文本。我的逻辑也可能是错误的,请指导我..

4

3 回答 3

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();




    }
}
}
于 2013-06-28T21:25:15.617 回答
2

选择 E.Odebugg 答案。

但是,如果您需要显示 a TextView3 秒钟然后隐藏,这里是如何在不使用TimerTask.

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

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

于 2013-06-28T21:32:09.270 回答
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”,因为它可以是保留字(颜色也表示它)......

于 2013-06-28T21:33:00.717 回答