Java 带有时间图标的动态按钮
作为我第一个问题的后续,乔治·穆利根友好地回答了我的问题 我正在尝试创建一个按钮,需要连续按下3次才能激活它。在每次点击之间,按钮上的倒计时显示其激活的剩余点击次数[因此,在第一次点击后,按钮将显示2,在第二次点击后,按钮将显示1,在第三次点击后,按钮变为绿色并显示active] 虽然应该有一个计时器在后台运行,或者在第一次点击后启动并在每次点击后重新启动的某种延迟,但第三次点击时按钮已经激活 当在下一次点击前达到某个最大延迟时,按钮应返回其原始状态,且必须重新启动,即在时间内连续点击3次,按钮才能激活 我该怎么办 到目前为止,我已经更改了1次点击的按钮,并设置了一个.postdayed()方法 尽管在第三次命中后,该功能应被覆盖和重置,并最终在第三次命中时被禁用 谁能告诉我如何做到这一点 有没有允许我这样做的除非声明 这是我的代码: MainActivity.javaJava 带有时间图标的动态按钮,java,android,xml,Java,Android,Xml,作为我第一个问题的后续,乔治·穆利根友好地回答了我的问题 我正在尝试创建一个按钮,需要连续按下3次才能激活它。在每次点击之间,按钮上的倒计时显示其激活的剩余点击次数[因此,在第一次点击后,按钮将显示2,在第二次点击后,按钮将显示1,在第三次点击后,按钮变为绿色并显示active] 虽然应该有一个计时器在后台运行,或者在第一次点击后启动并在每次点击后重新启动的某种延迟,但第三次点击时按钮已经激活 当在下一次点击前达到某个最大延迟时,按钮应返回其原始状态,且必须重新启动,即在时间内连续点击3次,按钮
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>
您应该使用计数器计算命中次数,检查命中情况,并使用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.
}
});
谢谢!!这正是我想要的。只需一个快捷按钮,当按钮变为活动状态时,我可以添加什么表达式使其保持活动状态或不再受.postdayed的影响?抱歉。我看到了这方面的缺陷,但我没有得到完整的解决方案:(