【问题标题】:countdown timers started before reaching the activities倒数计时器在到达活动之前开始
【发布时间】:2018-05-14 19:40:22
【问题描述】:

我的倒数计时器有问题,我的应用程序由带有项目的 recyclerview 组成,我正在尝试进行应用测验,所以当我在 recyclerview 中单击一个项目时,它会启动第一个活动,这是倒数计时器的第一个问题,当我单击按钮以从当前活动移动到它工作的另一个活动。但是当我点击后退按钮来回收查看其他 4 个问题时,他们会用他们的倒数计时器一个接一个地吃午饭。

first_five_Questions_questions_1

private RadioGroup radioGroup;
private RadioButton radioButton;
private Button btnDisplay;
private TextView mTextField;
private CountDownTimer myCount;
public long val;




@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_five_questions_q1);
    addListenerOnButton();

}


public void addListenerOnButton() {

    radioGroup = (RadioGroup) findViewById(R.id.Rd_Group_first_five_Questions_q1);
    btnDisplay = (Button) findViewById(R.id.Button_first_Five_Questions_q1);
    btnDisplay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // get selected radio button from radioGroup
            int selectedId =radioGroup.getCheckedRadioButtonId();
            myCount.cancel();
            // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);
            if(selectedId==R.id.Rd_btn_three__first_five_questions_q1)
            {
                val++;
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
                intent.putExtra("key", val);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }

            else
            {

                //When user Choose Wrong choice
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
                intent.putExtra("key", val);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

                finish();
            }
        }

    });

    myCount=new CountDownTimer(16000, 1000) {

        public void onTick(long millisUntilFinished) {

            mTextField = (TextView) findViewById(R.id.TimerClock_first_Five_Questions_Q1);
            mTextField.setText("Time left:"+millisUntilFinished / 1000);
           if(millisUntilFinished / 1000 == 5)
           {
               mTextField.setTextColor(Color.RED);
           }
        }


        public void onFinish() {
                                                                                       // When time finish go for mainActivity
            Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
            intent.putExtra("key", val);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            cancel();
            finish();
        }

    }.start();
}}

CustomRecyclerAdapterTests

private Context context;
private List<TestsUtils> TestsUTils;


public CustomRecyclerAdapterTests(Context context, List testutils) {

    this.context = context;
    this.TestsUTils = testutils;
}


@Override
public CustomRecyclerAdapterTests.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_list_item_tests, parent, false);
    CustomRecyclerAdapterTests.ViewHolder viewHolder = new CustomRecyclerAdapterTests.ViewHolder(v);
    return viewHolder;
}


@Override
public void onBindViewHolder(CustomRecyclerAdapterTests.ViewHolder holder, int position) {

    holder.itemView.setTag(TestsUTils.get(position));
    TestsUtils pu = TestsUTils.get(position);
    holder.Subject_Title.setText(pu.GetTestName());
    holder.Subject_Description.setText(pu.GetTestSubject());
}

@Override
public int getItemCount() {
    return TestsUTils.size();
}


public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView Subject_Title;
    public TextView Subject_Description;

    public ViewHolder(View itemView) {
        super(itemView);

        Subject_Title = (TextView) itemView.findViewById(R.id.Title_Single_List_Item_Tests);
        Subject_Description = (TextView) itemView.findViewById(R.id.Subject_Single_List_Item_Tests);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                switch (getPosition()) {
                    case 0:
                        TestsUtils obj1 = (TestsUtils) view.getTag();
                        Toast.makeText(view.getContext(), "Test-One", Toast.LENGTH_SHORT).show();
                        Intent intentone = new Intent(context, com.example.computer.policeproject.Questions_package.First_Five_Questions.first_five_Questions_questions_1.class);
                        context.startActivity(intentone);
                        break;

                    case 1:
                        TestsUtils obj2 = (TestsUtils) view.getTag();
                        Toast.makeText(view.getContext(), "Test-One", Toast.LENGTH_SHORT).show();
                        Intent intenttwo = new Intent(context, com.example.computer.policeproject.Questions_package.Second_Five_Questions.second_five_questions_questions_1.class);
                        context.startActivity(intenttwo);
                        break;
                }
            }
        });
    }
}

}

if(selectedId==R.id.Rd_btn_three__first_five_questions_q1)
            {
                val++;
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
                intent.putExtra("key", val);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }

            else
            {

                //When user Choose Wrong choice
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
                intent.putExtra("key", val);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

                finish();
            }

【问题讨论】:

  • 您需要根据当前位置从 RecyclerView 适配器启动问题活动。您可以将代码发布到您要启动第一个问题活动的地方吗?
  • 确定正在编辑
  • 即使我单击按钮午餐一和二,当我单击按钮背靠背recyclvew 时,其他 3 个问题 activitis 将与他们的倒计时一起午餐。以此类推,
  • 对不起。理解你的问题有点困难。你介意分享几张截图吗?
  • 我可以把项目兄弟发给你

标签: android android-layout android-recyclerview


【解决方案1】:

在您的活动中添加此方法

@Override
public void onBackPressed() {
  myCount.cancel();
}

这里是修改后的问题活动类。查看我粘贴 onBackPressed() 方法的位置,将其复制到所有问题活动中。

private RadioGroup radioGroup;
private RadioButton radioButton;
private Button btnDisplay;
private TextView mTextField;
private CountDownTimer myCount;
public long val;




@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_five_questions_q1);
    addListenerOnButton();

}


public void addListenerOnButton() {

    radioGroup = (RadioGroup) findViewById(R.id.Rd_Group_first_five_Questions_q1);
    btnDisplay = (Button) findViewById(R.id.Button_first_Five_Questions_q1);
    btnDisplay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // get selected radio button from radioGroup
            int selectedId =radioGroup.getCheckedRadioButtonId();
            myCount.cancel();
            // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);
            if(selectedId==R.id.Rd_btn_three__first_five_questions_q1)
            {
                val++;
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
                intent.putExtra("key", val);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }

            else
            {

                //When user Choose Wrong choice
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
                intent.putExtra("key", val);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

                finish();
            }
        }

    });

    myCount=new CountDownTimer(16000, 1000) {

        public void onTick(long millisUntilFinished) {

            mTextField = (TextView) findViewById(R.id.TimerClock_first_Five_Questions_Q1);
            mTextField.setText("Time left:"+millisUntilFinished / 1000);
           if(millisUntilFinished / 1000 == 5)
           {
               mTextField.setTextColor(Color.RED);
           }
        }


        public void onFinish() {
                                                                                       // When time finish go for mainActivity
            Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
            intent.putExtra("key", val);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            cancel();
            finish();
        }

    }.start();
}

@Override
public void onBackPressed() {
  super.onBackPressed();
  myCount.cancel();
}

}

【讨论】:

  • 在您的活动中。
  • 不,但这是活动的内置方法。按下后退按钮时将调用此方法。这将取消计时器。您在自己的按钮单击上调用了取消计时器
  • 但我的按钮点击直接进入 recyclerview ,而不是 previos 活动
  • 我会把项目发给你,你能帮我吗?
  • 是的,当然。我会试试的
猜你喜欢
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-05
  • 1970-01-01
相关资源
最近更新 更多