如何实现按钮只能点击一次后不可再次点击?

3
我有一个按钮。
    <Button 
    android:layout_above="@id/choice2"
    android:layout_centerHorizontal="true"
    android:textColor="#FFFF00"
    android:textSize="25sp"
    android:gravity="center"
    android:background="@drawable/loginbutton"
    android:layout_height="30dp"
    android:layout_width="fill_parent"
    android:layout_marginBottom="15dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:text="@string/q1a1"
    android:id="@+id/choice1">
</Button>

当你按下这个按钮时,它会将10添加到计数器/得分表中。如何使此按钮只能被按一次,然后在此之后不能再按?用户可以通过多次按下按钮来作弊,以增加他们的得分。

3个回答

12

在你的onCreate方法中,可以按照以下方式进行:

    final Button choice1 = (Button) findViewById(R.id.choice1);
    choice1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            choice1.setEnabled(false);
        }
    });

另一个不起作用,但这个符合我的目的并且工作得很好。谢谢! - codesomethin

4
您可以选择隐藏或禁用它。
Button mybutton;

    mybutton.setVisibility(View.GONE); // hide it
    mybutton.setClickable(false); // disable the ability to click it

0

在处理按钮的“正常”操作的代码中,当按钮被按下时禁用该按钮。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接