我最初使用此功能禁用我的按钮,但如何启用 一段时间后
button.setClickable(false);
实际上是尝试创建一个重新发送 otp 的按钮,以便最初 它应该被禁用,但在特定时间段后启用。
这是我的 otp.java 类
package com.avinashjain.hp.cc;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;
public class Otp extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp);
final ProgressBar simpleProgressBar = (ProgressBar)
findViewById(R.id.simpleProgressBar);
Button startButton = (Button) findViewById(R.id.button);
// perform click event on button
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// visible the progress bar
simpleProgressBar.setVisibility(View.VISIBLE);
}
});
}
}
这是activity_otp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context=".Otp"
android:orientation="vertical"
>
<com.goodiebag.pinview.Pinview
android:id="@+id/pinview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
app:cursorVisible="false"
app:forceKeyboard="true"
app:hint="0"
app:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:password="false"
app:pinBackground="@drawable/example_drawable"
app:pinHeight="35dp"
app:pinLength="7"
app:pinWidth="32dp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="169dp" />
<ProgressBar
android:id="@+id/simpleProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="160dp"
android:visibility="invisible" />
<Button
android:id="@+id/button"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="@string/verify_otp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="258dp" />
<Button
android:id="@+id/but_resend"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_marginStart="125dp"
android:layout_marginTop="20dp"
android:text="@string/resend_otp"
/>
</LinearLayout>
堆栈跟踪是
mBtn1.setEnabled(false); // The button is initially disabled //
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mBtn1.setEnabled(true); // The button is enabled by the timer afte 5 seconds //
}
});
}
}, 5000); // Set your time period here //
我希望那个按钮重复执行一定次数
谢谢 提前
最佳答案
您可以使用 timer
函数在定义的时间段内禁用按钮。这是一个例子:
public class Otp extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp);
final ProgressBar simpleProgressBar = (ProgressBar)
findViewById(R.id.simpleProgressBar);
Button startButton = (Button) findViewById(R.id.button);
// perform click event on button
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// visible the progress bar
simpleProgressBar.setVisibility(View.VISIBLE);
}
});
// Your Resend OTP Button Code is below //
Button ResendButton = (Button) findViewById(R.id.but_resend);
ResendButton.setEnabled(false); // The button is initially disabled //
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
ResendButton.setEnabled(true); // The button is enabled by the timer after 5 seconds //
}
});
}
}, 5000); // Set your time period here //
}}
关于java - 重复一定时间后启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50705799/