【问题标题】:Disable one button when another is clicked in Android [duplicate]在Android中单击另一个按钮时禁用一个按钮[重复]
【发布时间】:2016-12-28 00:36:00
【问题描述】:

我是 android 编程新手,我制作了一个测验应用程序来学习。在我的应用程序中有两个按钮,当用户单击另一个按钮时,我想禁用一个按钮。提前致谢。

这是两个按钮:

这是我的布局:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="15.2M"
        android:layout_marginTop="100dp"
        android:layout_marginRight="55dp"
        android:layout_marginLeft="55dp"
        android:id="@+id/submitWrong"
        android:onClick="submitWrong"
        android:background="#faa20a"
        />
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="13.3M"
        android:layout_toRightOf="@id/submitWrong"
        android:layout_marginTop="100dp"
        android:background="#faa20a"
        android:id="@+id/submitCorrect"
        android:onClick="submitCorrect"
        />

【问题讨论】:

标签: android android-button


【解决方案1】:

试试这样的:

public void submitWrong(View v) {
    // does something very interesting
    Button correctBtn = (Button) findViewById(R.id.submitCorrect);
    correctBtn.setEnabled(false);
}

public void submitCorrect(View v) {
    // does something very interesting
    Button wrongBtn = (Button) findViewById(R.id.submitCorrect);
    wrongBtn.setEnabled(false);
}

另见button docs

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    相关资源
    最近更新 更多