3

我以前搜索过很多,但我没有达到我的目标

我的问题是

我有一个带有计时器的列表视图,可以在每一行中倒计时,当我单击一行时,计数器会立即倒计时,但是当我单击另一行时,前一个计数器停止并且当前计数器倒计时,但它会将第二个计数为两个秒。

所以我的问题是:-

我想让与单击的行相关的计数器运行。不停止任何以前运行的计数器。

对不起我的英语不好,提前感谢。

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RecipeStepsActivity extends Activity {
    ListView list;
    int minutes;
    int seconds;
    int hours;
    int time;
    TextView fullTime;
    TextView step;
    String fullTimeString;
    String[] fullTimeArray;
    MediaPlayer myTen ;
    Activity context;
    @SuppressLint("ShowToast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_steps);
        context=this;

        list = (ListView) findViewById(R.id.listId);
        list.setAdapter(new RecipeStepsListAdapter(RecipeStepsActivity.this,R.layout.activity_recipe_steps_list_adapter,XmlHandler.HashMapOfSteps.get(Main.position),XmlHandler.HashMapOfTime.get(Main.position)));

        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int id,
                    long arg3) {
                // TODO Auto-generated method stub
                final String item = (String) parent.getItemAtPosition(id);
                fullTimeArray = new String[3];
                fullTimeArray = item.split(":");
                Toast.makeText(RecipeStepsActivity.this,
                        "h "+fullTimeArray[0]+":m "+fullTimeArray[1]+":s "+fullTimeArray[2], Toast.LENGTH_SHORT).show();

                step = (TextView)findViewById(R.id.step);
                fullTime = (TextView)findViewById(id);

                hours = Integer.parseInt(fullTimeArray[0]);
                minutes = Integer.parseInt(fullTimeArray[1]);
                seconds = Integer.parseInt(fullTimeArray[2]);
                time = (hours*60*60)+(minutes*60)+seconds;
                createCounterDownTimer();

            }
        });
    }




    public void createCounterDownTimer(){
        runOnUiThread( new Runnable() {
            public void run() {
                // TODO Auto-generated method stub


        new CountDownTimer(time * 1000, 1000) {

            @SuppressLint("NewApi")
            @Override
            public void onTick(long millisUntilFinished) {


                if(hours>0 && minutes == 0 && seconds == 0){
                    hours -- ;

                    fullTime.setText(hours+":"+minutes+":"+seconds);
                    fullTime.animate();
                    minutes = 60;
                }
                if( minutes>0 && seconds == 0){
                    minutes--;
                    fullTime.setText(hours+":"+minutes+":"+seconds);
                    fullTime.animate();
                    seconds = 60;
                    //seconds --;
                }
                if(seconds>0 ){
                    seconds--;
                    fullTime.setText(hours+":"+minutes+":"+seconds);
                    fullTime.animate();

                }

            }

            @Override
            public void onFinish() {
                // TODO Auto-generated method stub

            myTen = MediaPlayer.create(RecipeStepsActivity.this, R.raw.soundd);
            myTen.start();
            //this is a run for the ui ajax containing alertdialog
            runOnUiThread( new Runnable() {
                @SuppressWarnings("deprecation")
                public void run() {
                    fullTime.setText("time up!");
                    //showNotification();
                    AlertDialog alertDialog = new AlertDialog.Builder(RecipeStepsActivity.this).create();
                    alertDialog.setTitle("time up");
                    alertDialog.setMessage(step.getText().toString()+" is finished");
                    alertDialog.setButton("ok", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            myTen.stop();
                            dialog.cancel();
                        }
                    });

                    alertDialog.show();



                }
            });
            }

        }.start();

            }
        });
    }
    @Override

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

2 回答 2

2

通过创建一个名为“createCounterDownTimer”的方法,我已经解决了在 2 秒内计数秒的问题,以及停止前一个计数器的问题,我将列表视图中使用的所有文本视图都放在了这个方法中,并使用了“id “当前单击的行,并使用“CountDownTimer(时间以毫秒为单位,间隔)” ...

这是活动的代码

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RecipeStepsActivity extends Activity {
ListView list;
int minutes;
int seconds;
int hours;
TextView fullTime;
TextView step;
String fullTimeString;
String[] fullTimeArray;
MediaPlayer myTen;
Activity context;
long init = 0, now;
AdapterView<?> parent1;

@SuppressLint("ShowToast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe_steps);
    context = this;

    list = (ListView) findViewById(R.id.listId);
    list.setAdapter(new RecipeStepsListAdapter(RecipeStepsActivity.this,
            R.layout.activity_recipe_steps_list_adapter,
            XmlHandler.HashMapOfSteps.get(Main.position),
            XmlHandler.HashMapOfTime.get(Main.position)));

    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int id,long arg3) {
        parent1=parent;
        final String item = (String) parent1.getItemAtPosition(id);
        fullTimeArray = new String[3];
        fullTimeArray = item.split(":");
        Toast.makeText(RecipeStepsActivity.this,
                "h " + fullTimeArray[0] + ":m " + fullTimeArray[1]
                        + ":s " + fullTimeArray[2], Toast.LENGTH_SHORT).show();
            hours = Integer.parseInt(fullTimeArray[0]);
            minutes = Integer.parseInt(fullTimeArray[1]);
            seconds = Integer.parseInt(fullTimeArray[2]);
            Long times = ((hours * 60L * 60L) + (minutes * 60L) + seconds) * 1000;
            createCounterDownTimer(times,id);

        }
    });

}

public void createCounterDownTimer(final Long times,final int id) {
    runOnUiThread(new Runnable() {
        public void run() {

            new CountDownTimer(times, 1000) {

                @SuppressLint("NewApi")
                @Override
                public void onTick(long ms) {
                    step = (TextView) findViewById(R.id.step);
                    fullTime = (TextView) findViewById(id);

                    int seconds = (int) (ms / 1000) % 60;
                    int minutes = (int) ((ms / (1000 * 60)) % 60);
                    int hours = (int) ((ms / (1000 * 60 * 60)) % 24);
                    System.out.println(hours + ": " + minutes + ": "
                            + seconds);
                    fullTime.setText(hours + ": " + minutes + ": "
                            + seconds);
                }

                @Override
                public void onFinish() {
                    // TODO Auto-generated method stub

                    myTen = MediaPlayer.create(RecipeStepsActivity.this,
                            R.raw.soundd);
                    myTen.start();
                    // this is a run for the ui ajax containing alertdialog

                    fullTime.setText("time up!");
                    // showNotification();
                    AlertDialog alertDialog = new AlertDialog.Builder(
                            RecipeStepsActivity.this).create();
                    alertDialog.setTitle("time up");
                    alertDialog.setMessage(step.getText()
                            .toString() + " is finished");
                    alertDialog.setButton("ok",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(
                                        DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method
                                    // stub
                                    myTen.stop();
                                    dialog.cancel();

                                }
                            });

                    alertDialog.show();

                }

            }.start();

        }
    });
}

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

}

于 2013-11-18T09:46:31.777 回答
0

使用 AsyncTask 概念。通过使用它,您可以运行多个线程而不会干扰 UI 线程。只需谷歌它,你就会得到很多关于 AsyncTask 概念的教程。

于 2013-11-13T12:57:56.753 回答