在 android studio 中用按钮上显示的计时器锁定按钮
Lock a Button With timer displayed on the button in android studio
我正在开发一个 android 应用程序,我在其中展示了一个奖励视频广告并希望该按钮锁定 10 分钟,我希望在按钮上使用它后显示一个计时器,这是我的 java 文件
watch_video_buuton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(Home_page.this,"Please tap more times to load the ad", Toast.LENGTH_LONG).show();
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
else {
loadRewardedVideoAd();
}
}
});
@Override
public void onRewardedVideoAdLoaded() {
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
@Override
public void onRewarded(RewardItem rewardItem) {
Toast.makeText(Home_page.this,"Congratulations, you will get 1000 points next time you open the app ", Toast.LENGTH_LONG).show();
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int n = myIntValue+1000;
editor.putInt("your_int_key", n);
editor.commit();
user_id_child.child("scores").setValue(+n);
}
您可以为您的按钮使用以下代码:
public void button_OnClick(View view) {
Button button=findViewById(R.id.button);
button.setEnabled(false);
final long startTime= Calendar.getInstance().getTime().getTime();
final Timer timer=new Timer();
final Activity activity = this;
TimerTask task=new TimerTask() {
@Override
public void run() {
long curentTime=Calendar.getInstance().getTime().getTime();
long elapsedTime=curentTime-startTime;
ToastTime(10*60*1000, elapsedTime);
if(curentTime-startTime>=10*60*1000){ //10 minutes in millisecond
timer.cancel();
EnableButton();
}
}
};
timer.schedule(task, 500, 2000);
}
在哪里
private void ToastTime(long totalTime, long elapsedTime){
final int remainInSec = (int)(totalTime - elapsedTime) / 1000;
if(remainInSec>0) {
final Activity activity = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, String.valueOf(remainInSec) + " seconds to enable button", Toast.LENGTH_SHORT).show();
}
});
}
}
private void EnableButton(){
Button button=findViewById(R.id.button);
button.setEnabled(true);
}
或简化:
public void button_OnClick(View view) {
final Button button=findViewById(R.id.button);
button.setEnabled(false);
final long startTime= Calendar.getInstance().getTime().getTime();
final Timer timer=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
long curentTime=Calendar.getInstance().getTime().getTime();
long elapsedTime=curentTime-startTime;
ToastTime(10*60*1000, elapsedTime);
if(curentTime-startTime>=10*60*1000){ //10 minutes in millisecond
timer.cancel();
button.setEnabled(true);
}
}
};
timer.schedule(task, 500, 2000);
}
private void ToastTime(long totalTime, long elapsedTime){
final int remainInSec = (int)(totalTime - elapsedTime) / 1000;
if(remainInSec>0) {
final Activity activity = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, String.valueOf(remainInSec) + " seconds to enable button", Toast.LENGTH_SHORT).show();
}
});
}
}
编辑
如果出现 many run on main thread
错误,请尝试以下操作:
声明一个全局变量:
boolean isLooperCalled;
现在使用这个:
public void button_OnClick(View view) {
final Button button=findViewById(R.id.button);
button.setEnabled(false);
isLooperCalled=false; //It's new
final long startTime= Calendar.getInstance().getTime().getTime();
final Timer timer=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
if(isLooperCalled==false){ //It's new
Looper.prepare();
isLooperCalled=true;
}
long curentTime=Calendar.getInstance().getTime().getTime();
long elapsedTime=curentTime-startTime;
ToastTime(10*60*1000, elapsedTime);
if(curentTime-startTime>=10*60*1000){ //10 minutes in millisecond
timer.cancel();
button.setEnabled(true);
}
}
};
timer.schedule(task, 500, 2000);
}
private void ToastTime(long totalTime, long elapsedTime){
final int remainInSec = (int)(totalTime - elapsedTime) / 1000;
if(remainInSec>0) { //It's edited
Toast.makeText(this, String.valueOf(remainInSec) + " seconds to enable button", Toast.LENGTH_SHORT).show();
}
}
如果错误仍然存在,请将 timer.schedule(task, 500, 2000)
中的时间步长从 2000
增加到 10000
的 5000
(5 秒或 10 秒)。
我正在开发一个 android 应用程序,我在其中展示了一个奖励视频广告并希望该按钮锁定 10 分钟,我希望在按钮上使用它后显示一个计时器,这是我的 java 文件
watch_video_buuton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(Home_page.this,"Please tap more times to load the ad", Toast.LENGTH_LONG).show();
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
else {
loadRewardedVideoAd();
}
}
});
@Override
public void onRewardedVideoAdLoaded() {
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
@Override
public void onRewarded(RewardItem rewardItem) {
Toast.makeText(Home_page.this,"Congratulations, you will get 1000 points next time you open the app ", Toast.LENGTH_LONG).show();
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int n = myIntValue+1000;
editor.putInt("your_int_key", n);
editor.commit();
user_id_child.child("scores").setValue(+n);
}
您可以为您的按钮使用以下代码:
public void button_OnClick(View view) {
Button button=findViewById(R.id.button);
button.setEnabled(false);
final long startTime= Calendar.getInstance().getTime().getTime();
final Timer timer=new Timer();
final Activity activity = this;
TimerTask task=new TimerTask() {
@Override
public void run() {
long curentTime=Calendar.getInstance().getTime().getTime();
long elapsedTime=curentTime-startTime;
ToastTime(10*60*1000, elapsedTime);
if(curentTime-startTime>=10*60*1000){ //10 minutes in millisecond
timer.cancel();
EnableButton();
}
}
};
timer.schedule(task, 500, 2000);
}
在哪里
private void ToastTime(long totalTime, long elapsedTime){
final int remainInSec = (int)(totalTime - elapsedTime) / 1000;
if(remainInSec>0) {
final Activity activity = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, String.valueOf(remainInSec) + " seconds to enable button", Toast.LENGTH_SHORT).show();
}
});
}
}
private void EnableButton(){
Button button=findViewById(R.id.button);
button.setEnabled(true);
}
或简化:
public void button_OnClick(View view) {
final Button button=findViewById(R.id.button);
button.setEnabled(false);
final long startTime= Calendar.getInstance().getTime().getTime();
final Timer timer=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
long curentTime=Calendar.getInstance().getTime().getTime();
long elapsedTime=curentTime-startTime;
ToastTime(10*60*1000, elapsedTime);
if(curentTime-startTime>=10*60*1000){ //10 minutes in millisecond
timer.cancel();
button.setEnabled(true);
}
}
};
timer.schedule(task, 500, 2000);
}
private void ToastTime(long totalTime, long elapsedTime){
final int remainInSec = (int)(totalTime - elapsedTime) / 1000;
if(remainInSec>0) {
final Activity activity = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(activity, String.valueOf(remainInSec) + " seconds to enable button", Toast.LENGTH_SHORT).show();
}
});
}
}
编辑
如果出现 many run on main thread
错误,请尝试以下操作:
声明一个全局变量:
boolean isLooperCalled;
现在使用这个:
public void button_OnClick(View view) {
final Button button=findViewById(R.id.button);
button.setEnabled(false);
isLooperCalled=false; //It's new
final long startTime= Calendar.getInstance().getTime().getTime();
final Timer timer=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
if(isLooperCalled==false){ //It's new
Looper.prepare();
isLooperCalled=true;
}
long curentTime=Calendar.getInstance().getTime().getTime();
long elapsedTime=curentTime-startTime;
ToastTime(10*60*1000, elapsedTime);
if(curentTime-startTime>=10*60*1000){ //10 minutes in millisecond
timer.cancel();
button.setEnabled(true);
}
}
};
timer.schedule(task, 500, 2000);
}
private void ToastTime(long totalTime, long elapsedTime){
final int remainInSec = (int)(totalTime - elapsedTime) / 1000;
if(remainInSec>0) { //It's edited
Toast.makeText(this, String.valueOf(remainInSec) + " seconds to enable button", Toast.LENGTH_SHORT).show();
}
}
如果错误仍然存在,请将 timer.schedule(task, 500, 2000)
中的时间步长从 2000
增加到 10000
的 5000
(5 秒或 10 秒)。