【问题标题】:Why negativeButton is "invisible"?为什么negativeButton是“不可见的”?
【发布时间】:2021-10-03 05:52:24
【问题描述】:

我正在尝试构建一个具有生物特征身份验证(指纹)的应用程序,但我在使用否定按钮时遇到了一些问题。 该按钮有效,但由于某种原因完全不可见。 This is how the app shows

And this is how it sees when you prees the button. As you can see, it exist, but i don't know how to make it visible

我在 Java 中使用 BiometricPrompt 和 BiometricManager。

编辑:似乎该按钮在不属于我的任何其他手机中正常显示 我用的是小米红米 Note 8。

但这是我正在使用的代码:

private void initViews()
{
    biometricManager = BiometricManager.from(this);
    passwordEditText=findViewById(R.id.passwordText);
    loginButton=findViewById(R.id.loginButton);
    switch (biometricManager.canAuthenticate()) {
        case BiometricManager.BIOMETRIC_SUCCESS:
            Log.d("MY_APP_TAG", "App can authenticate using biometrics.");
            break;
        case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
            Log.e("MY_APP_TAG", "No biometric features available on this device.");
            break;
        case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
            Log.e("MY_APP_TAG", "Biometric features are currently unavailable.");
            break;
        case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
            Log.e("MY_APP_TAG", "The user hasn't associated " +
                    "any biometric credentials with their account.");
            break;
    }
    executor = ContextCompat.getMainExecutor(this);
    biometricPrompt = new BiometricPrompt(EnterYourPassActivity.this,
            executor, new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode,
                                          @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);
            if(errString.equals("Use account password"))
            {
                passwordEditText.setVisibility(View.VISIBLE);
            }
            else
            {
                Log.d("MY_APP_TAG",""+errString);
                Toast.makeText(getApplicationContext(),
                        "Authentication error: " + errString, Toast.LENGTH_SHORT)
                        .show();
            }
        }

        @Override
        public void onAuthenticationSucceeded(
                @NonNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            Toast.makeText(getApplicationContext(),
                    "Authentication succeeded!", Toast.LENGTH_SHORT).show();
            Intent seeingFiles = new Intent(EnterYourPassActivity.this, SeeingFilesActivity.class);
            startActivity(seeingFiles);
        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            Toast.makeText(getApplicationContext(), "Authentication failed",
                    Toast.LENGTH_SHORT)
                    .show();
        }
    });

    promptInfo = new BiometricPrompt.PromptInfo.Builder()
            .setTitle("Biometric login for my app")
            .setSubtitle("Log in using your biometric credential")
            .setNegativeButtonText("Use account password")
            .build();

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            biometricPrompt.authenticate(promptInfo);
        }
    });
}

【问题讨论】:

  • 您可以通过 xml(visible 属性,如果我没记错的话)或代码(button.setVisibility(View.VISIBLE))设置按钮 visibility。最后检查按钮的颜色是否与背景区域的颜色相同。
  • 请通过添加代码来编辑您的问题,例如您如何构建对话框。看看这是否适合您:dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setBackground(R.color.yourcolor );
  • 谢谢,帖子已经编辑了
  • 我在三星 Galaxy S8、Android 9 上看到了同样的问题。没有要操作的对话框对象,所以我不清楚如何访问此按钮。

标签: java android android-biometric-prompt android-biometric


【解决方案1】:

尝试以这种方式更改negativeButton文本:
重要 - 将字符串放入资源中:

    <string name="negative_button_text"><![CDATA[<font color=\'#48a134\'>Your text at given color</font>]]></string>

如您所见,您可以将文本颜色设置为十六进制。 现在将negativeText放入BiometricPrompt,如下:

val negativeButtonText = getString(R.string.negative_button_text)
val promptInfo = BiometricPrompt.PromptInfo.Builder()
    .setTitle("title")
    .setDescription("description")
    .setNegativeButtonText(fromHtml(negativeButtonText))
    .build()

fun fromHtml(html: String): Spanned {
    return HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY)
}

在给定的示例中,negativeText 为绿色,提示符看起来像 THIS

【讨论】:

    猜你喜欢
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2016-08-13
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多