作为我的第一个问题的后续行动,并由George Mulligan友好回答,
我正在尝试创建一个需要连续按3次才能被激活的按钮。在每次击中之间,按钮上的倒计时显示剩余的命中数,使其变为活动状态[因此,在第一次击中后,按钮将显示2,第二次显示1后,第三次点亮绿色并显示活动状态] < / p>
虽然应该有一个在后台运行的计时器或在第一次点击后启动的某种延迟,并在按钮已经变为活动状态之后重新启动,但第三次除外。
当在下次命中之前达到某个最大延迟时,该按钮将返回其原始状态,并且必须从头开始,即在时间内连续3次命中以使按钮变为活动状态。
我该怎么做?
到目前为止,我已将按钮更改为1次点击并设置.postdelayed()方法。
虽然这应该被覆盖并在另一次击中后重置,并最终在第3次击中时停用。
谁能告诉我如何实现这一目标?
是否有允许我这样做的UNLESS声明?
这是我的代码:
MainActivity.java
package button.tutorials.suvendu.com.mybuttontutorial;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
Timer timer;
MyTimerTask myTimerTask;
Button CoolButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CoolButton=(Button)findViewById(R.id.btnchangedisp);
CoolButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View vw) {
CoolButton.setText(getString(R.string.FirstClick));
CoolButton.postDelayed(new Runnable() {
@Override
public void run() {
CoolButton.setText(getString(R.string.button));
}
}, 1000);
//THIS IS HOW FAR I GOT. THE REST IS STANDARD CODE.
timer = new Timer();
myTimerTask = new MyTimerTask();
}
});
}
class MyTimerTask extends TimerTask {
@Override
public void run(){
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和它引用的xml文件:
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="button.tutorials.suvendu.com.mybuttontutorial.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/btnchangedisp"
android:layout_centerHorizontal="true"
android:layout_marginTop="128dp" />
</RelativeLayout>
答案 0 :(得分:1)
您应该使用计数器计算您的命中和检查命中条件并使用postDelayed方法。
int count = 0;
CoolButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count = count + 1;// initialise int variable global like int count = 0;
if (count == 3) {
count = 0;// Reset the counter when you got activate.
Toast.makeText(MainActivity.this, "Your Button is Activated", Toast.LENGTH_SHORT).show();
// write code here of activate button color change or methods and whatever you want .
} else if (count == 2) {
Toast.makeText(MainActivity.this, "Your click one more time to activate button", Toast.LENGTH_SHORT).show();
}else if (count == 1) {
Toast.makeText(MainActivity.this, "Your click two more time to activate button", Toast.LENGTH_SHORT).show();
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
count = 0;
}
}, 5000);// this is you delay time , with in 5 second you have to click 3 time otherwise it will again reset counter zero.
// you can set you time interval to restart counter.
}
});