Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Timer类针对按钮单击显示圆形进度条_Java_Android_Class_Timer_Progress Bar - Fatal编程技术网

Java 使用Timer类针对按钮单击显示圆形进度条

Java 使用Timer类针对按钮单击显示圆形进度条,java,android,class,timer,progress-bar,Java,Android,Class,Timer,Progress Bar,我用两个输入框和一个总计按钮对简单计算器进行了编码,我要做的是在按下总计按钮后显示结果之前,先显示一个圆形进度条3秒钟,下面是我所做的 包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

我用两个输入框和一个总计按钮对简单计算器进行了编码,我要做的是在按下总计按钮后显示结果之前,先显示一个
圆形进度条3秒钟,下面是我所做的

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

如果我理解正确,您要做的是显示一个ProgressDialog来模拟您的计算器正在进行一些计算。Timer类不是您想要使用的。基本上,您需要等待3秒钟(在此期间ProgressDialog将可见),然后通过使用结果设置TextView来更新UI。正如您可能知道的,您不能直接从后台线程修改UI线程,并且不应该使UI线程处于休眠状态。因此,AsyncTask是您的最佳选择。下面是一个示例代码,允许您在3秒钟内显示ProgressDialog。您应该在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();




    }
}
}
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.widget.RelativeLayout;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
进展性帕金森病;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd=新进度对话框(本);
pd.setTitle(getString(“Title”);
pd.SetUndeterminate(真);
pd.show();
新建MyAsycntTask().execute();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
私有类MyAsycntTask扩展了AsyncTask{
@凌驾
受保护的空位背景(空位…空位){
试一试{
睡眠(3000);
}捕捉(中断异常e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
pd.hide();
}
}
}

如果我理解正确,您要做的是显示一个ProgressDialog来模拟您的计算器正在进行一些计算。Timer类不是您想要使用的。基本上,您需要等待3秒钟(在此期间ProgressDialog将可见),然后通过使用结果设置TextView来更新UI。正如您可能知道的,您不能直接从后台线程修改UI线程,并且不应该使UI线程处于休眠状态。因此,AsyncTask是您的最佳选择。下面是一个示例代码,允许您在3秒钟内显示ProgressDialog。您应该在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();




    }
}
}
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.widget.RelativeLayout;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
进展性帕金森病;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd=新进度对话框(本);
pd.setTitle(getString(“Title”);
pd.SetUndeterminate(真);
pd.show();
新建MyAsycntTask().execute();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
私有类MyAsycntTask扩展了AsyncTask{
@凌驾
受保护的空位背景(空位…空位){
试一试{
睡眠(3000);
}捕捉(中断异常e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
pd.hide();
}
}
}

用E.Odebug回答

但是,如果您需要显示
文本视图
3秒钟,然后隐藏,下面介绍如何在不使用
TimerTask
的情况下执行此操作

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

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

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




    }
}
}
但是,如果您需要显示
文本视图
3秒钟,然后隐藏,下面介绍如何在不使用
TimerTask
的情况下执行此操作

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

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

因此我认为您的代码在loadprogressbar()中是错误的。应该是:

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




    }
}
}
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”的名称,因为它可以是保留字(颜色也表示它).

所以我认为你的代码在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”,因为它可以是一个保留字(颜色也表示它).

这些答案中有你想要做的吗?如果是这样的话,你能把这个问题标记为已回答吗?当然,我会的,我是android新手,只是谷歌异步任务,这个答案似乎很合适,如果对我有效,我会标记:)这些答案中有你想做的吗?如果是这样的话,你能把这个问题标记为已回答吗?当然,我会的,因为我是android新手,只是在谷歌上搜索AsyncTask,这个答案