如何在Kotlin中禁用按钮?

时间:2019-04-12 05:53:18

标签: android kotlin

我创建了2个活动,第一个活动是我将其用作启动屏幕,现在在第二个活动中,我在停用按钮时遇到了困难,我将代码留给您理解

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".TemaCalcActivity">

    <Spinner
            android:layout_width="144dp"
            android:layout_height="39dp"
            android:id="@+id/spnDiferencia" app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginStart="8dp" app:layout_constraintStart_toEndOf="@+id/spnMP"
            android:layout_marginEnd="8dp" android:layout_marginTop="24dp"
            app:layout_constraintTop_toBottomOf="@+id/textView5"/>

    <Button
            android:text="OK"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/button" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="120dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="120dp" android:layout_marginTop="32dp"
            app:layout_constraintTop_toBottomOf="@+id/spnMP" android:background="@drawable/button_effect"
            android:textSize="18sp" android:textStyle="bold" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintVertical_bias="0.026" android:onClick="onBotonFinalizar" android:enabled="false"/>

</android.support.constraint.ConstraintLayout>

Main2Activity.kt

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        spnDiferencia.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                when (position) {
                    0 -> { button.isEnabled = true}
                    1 -> { button.isEnabled = false}
        }
}

我的错误是整个应用程序已停止,然后重新启动,这是重复操作。

button.isEnabled = true
// or
button.isClickable = true

警告---> 按钮== btnCalcular

--- 错误日志 ---

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mrl.fr.tuto, PID: 22066
    java.lang.IllegalStateException: btnCalcular must not be null
        at com.mrl.fr.tuto.Main2Activity$onCreate$5.onItemSelected(Main2cActivity.kt:128)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:919)
        at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:908)
        at android.widget.AdapterView.access$300(AdapterView.java:53)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:878)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5631)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

2 个答案:

答案 0 :(得分:3)

您尝试过

myButton.setEnabled(false);

答案 1 :(得分:1)

只需禁用按钮即可。

button.isEnabled = false
button.isClickable = false

并且如果您要禁用按钮,并且希望将其颜色和背景设为灰色,则可以执行以下操作-

fun markButtonDisable(button: Button) {
    button?.isEnabled = false
    button?.setTextColor(ContextCompat.getColor(textView.context, R.color.white))
    button?.setBackgroundColor(ContextCompat.getColor(textView.context, R.color.greyish))
}