gpt4 book ai didi

android - 在 Android Kotlin 中无法访问 dialog.getButton()

转载 作者:行者123 更新时间:2023-12-02 13:10:43 27 4
gpt4 key购买 nike

我正在尝试将系统警报对话框按钮的颜色更改为自定义颜色。我一直在尝试使用这个:

dialog.create().getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getColor(R.color.bluish_grey))
但是应用程序崩溃了。
另外, dialog.getButton不起作用。只有 set...() 方法。
更重要的是,我也尝试过使用 .getButton()dialog.show() 之后,但应用程序崩溃并显示以下错误日志。
我的警报对话框不是自定义的,它只是一个系统对话框。那么,是否可以在不使用 XML 的情况下将警报对话框的按钮颜色设置为自定义颜色?
这就是我想要实现的。
My desired result
这是我目前拥有的:
The current result
更新:错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: no.company.app, PID: 26643
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setTextColor(int)' on a null object reference
at no.meshtech.demokit.view.activity.MainActivity.showCancelDialog(MainActivity.kt:402)
at no.meshtech.demokit.view.activity.MainActivity.startUpdate(MainActivity.kt:317)
at no.meshtech.demokit.view.activity.MainActivity.onClick(MainActivity.kt:67)
at android.view.View.performClick(View.java:7869)
at android.widget.TextView.performClick(TextView.java:14958)
at android.view.View.performClickInternal(View.java:7838)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29362)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8019)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

更新 2 :
我创建警报的代码:
   override fun showCompletedDialog() {
DfuBaseService.PROGRESS_COMPLETED

val dialog = AlertDialog.Builder(this)

.setTitle(getString((R.string.dfu_completed_title)))
.setMessage(getString(R.string.dfu_completed_message))
.setPositiveButton(R.string.dfu_completed_ok) { dialog, _ ->
transaction(listFragment, R.id.cvCentral)
supportFragmentManager.findFragmentById(R.id.upperContainer)?.let {
supportFragmentManager.beginTransaction().remove(it).commit()
setNavigationTitle(getString(R.string.title_header_devices))
}
upperContainer.visibility = View.GONE

Handler().postDelayed({
onBackPressed()
dialog.dismiss()
}, 400)
}


dialog.setCancelable(false)
dialog.show()
}

最佳答案

您应该添加 setTextColor show() 之后;

    val alertDialog = AlertDialog.Builder(this).create()
alertDialog.setTitle(getString((R.string.app_name)))
alertDialog.setMessage(getString(R.string.app_name))
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "RESUME"
) { dialog, which -> dialog.dismiss() }

alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "ABORT"
) { dialog, which -> dialog.dismiss() }

alertDialog.show()
alertDialog.setCancelable(false)
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(this,R.color.colorPrimary))
alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(this,R.color.colorAccent))

关于android - 在 Android Kotlin 中无法访问 dialog.getButton(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63411153/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com