我想要一個 OTP 螢屏。我成功創建了 OTP 螢屏。在此之下,我想要一個重新發送 otp 按鈕和 1 個計時器。如果沒有輸入,則計時器應在按鈕未處于焦點時啟動。輸入 otp 后,計時器應停止。我有一個計時器功能,啟動我從 onCreate 活動結束時呼叫的計時器。它應該取消發送按鈕的焦點,當到期時,應該將其聚焦。但是計時器沒有顯示在螢屏上,一段時間后會顯示按鈕。
我想要
- 計時器以笨拙的格式顯示。我只想要幾秒鐘。
- 該按鈕現在是不可見的,它應該是可見的,但在失焦時變暗
代碼:
public class OTPActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private EditText editText1, editText2, editText3, editText4;
private EditText[] editTexts;
CountDownTimer cTimer = null;
TextView tv;
Button resend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otpactivity2);
tv = (TextView) findViewById(R.id.timer);
resend = (Button) findViewById(R.id.ResendOTP);
editText1 = (EditText) findViewById(R.id.otpEdit1);
editText2 = (EditText) findViewById(R.id.otpEdit2);
editText3 = (EditText) findViewById(R.id.otpEdit3);
editText4 = (EditText) findViewById(R.id.otpEdit4);
editTexts = new EditText[]{editText1, editText2, editText3, editText4};
editText1.addTextChangedListener(new PinTextWatcher(0));
editText2.addTextChangedListener(new PinTextWatcher(1));
editText3.addTextChangedListener(new PinTextWatcher(2));
editText4.addTextChangedListener(new PinTextWatcher(3));
editText1.setOnKeyListener(new PinOnKeyListener(0));
editText2.setOnKeyListener(new PinOnKeyListener(1));
editText3.setOnKeyListener(new PinOnKeyListener(2));
editText4.setOnKeyListener(new PinOnKeyListener(3));
startTimer();
resend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTimer();
}
});
}
void startTimer() {
resend.setEnabled(false);
cTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText("seconds remaining: " String.valueOf(millisUntilFinished/1000));
}
public void onFinish() {
tv.setText("Re send OTP!");
resend.setEnabled(true);
}
};
cTimer.start();
}
//cancel timer
void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public class PinTextWatcher implements TextWatcher {
private int currentIndex;
private boolean isFirst = false, isLast = false;
private String newTypedString = "";
PinTextWatcher(int currentIndex) {
this.currentIndex = currentIndex;
if (currentIndex == 0)
this.isFirst = true;
else if (currentIndex == editTexts.length - 1)
this.isLast = true;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
newTypedString = s.subSequence(start, start count).toString().trim();
}
@Override
public void afterTextChanged(Editable s) {
String text = newTypedString;
/* Detect paste event and set first char */
if (text.length() > 1)
text = String.valueOf(text.charAt(0)); // TODO: We can fill out other EditTexts
editTexts[currentIndex].removeTextChangedListener(this);
editTexts[currentIndex].setText(text);
editTexts[currentIndex].setSelection(text.length());
editTexts[currentIndex].addTextChangedListener(this);
if (text.length() == 1)
moveToNext();
else if (text.length() == 0)
moveToPrevious();
}
private void moveToNext() {
if (!isLast)
editTexts[currentIndex 1].requestFocus();
if (isAllEditTextsFilled() && isLast) { // isLast is optional
editTexts[currentIndex].clearFocus();
hideKeyboard();
}
}
private void moveToPrevious() {
if (!isFirst)
editTexts[currentIndex - 1].requestFocus();
}
private boolean isAllEditTextsFilled() {
for (EditText editText : editTexts)
if (editText.getText().toString().trim().length() == 0)
return false;
return true;
}
private void hideKeyboard() {
if (getCurrentFocus() != null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
}
public class PinOnKeyListener implements View.OnKeyListener {
private int currentIndex;
PinOnKeyListener(int currentIndex) {
this.currentIndex = currentIndex;
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
if (editTexts[currentIndex].getText().toString().isEmpty() && currentIndex != 0)
editTexts[currentIndex - 1].requestFocus();
}
return false;
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
>
<androidx.cardview.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@ id/card_view"
android:layout_width="180dp"
card_view:cardElevation="4dp"
android:gravity="center"
android:layout_height="60dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<EditText
android:id="@ id/otpEdit1"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:gravity="center"
android:layout_weight="1"
android:layout_width="2dp"
android:layout_height="wrap_content"
/>
<EditText
android:id="@ id/otpEdit2"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:gravity="center"
android:layout_weight="1"
android:layout_width="2dp"
android:layout_height="wrap_content"
/>
<EditText
android:id="@ id/otpEdit3"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:gravity="center"
android:layout_weight="1"
android:layout_width="2dp"
android:layout_height="wrap_content"
/>
<EditText
android:id="@ id/otpEdit4"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:gravity="center"
android:layout_weight="1"
android:layout_width="2dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<TextView
android:id="@ id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timer"
android:layout_marginLeft="10dp"/>
<Button
android:id="@ id/ResendOTP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ResendOTP" />
</LinearLayout>
uj5u.com熱心網友回復:
根據您的倒計時設定,onTick 方法在 1 秒后呼叫。因為該按鈕在活動開始時可見。
此外,如果您只想在倒數計時器中顯示秒數,請使用以下語法:
void startTimer() {
tv = (TextView) findViewById(R.id.timer);
cTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText("seconds remaining: " String.valueOf(millisUntilFinished/1000));
}
public void onFinish() {
tv.setText("Re send OTP!");
resend.setEnabled(true);
}
};
cTimer.start();
}
您不需要在每個滴答聲上設定隱形按鈕,它會增加 ui 事件。在啟動計時器之前,您需要在 onCreate 方法或任何地方設定不可見的按鈕,如下所示:
resend.setVisibility(View.INVISIBLE);
startTimer();
同樣出于調暗目的,您需要更改按鈕顏色并使用 setEnabled 方法啟用和禁用按鈕單擊。
uj5u.com熱心網友回復:
您應該使用setEnabled按鈕而不是設定可見性。
onTick您可以在startTimer函式中設定一次,而不是禁用每次都會呼叫的按鈕。只需在onTick. 如果需要,您還可以在 XML 中設定這些初始值。
由于您想顯示秒,而不是new SimpleDateFormat("mm:ss:SS").format(new Date( millisUntilFinished))您可以簡單地millisUntilFinished / 1000將其設定為您的 TextView。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/339543.html
上一篇:如何在不回想該活動的API呼叫的情況下為橫向和縱向模式使用不同的設計?
下一篇:我無法參考存在的字串
