如何使按钮按下一次然后不再可按下?

Posted

技术标签:

【中文标题】如何使按钮按下一次然后不再可按下?【英文标题】:How to make a button press once and then not pressable anymore? 【发布时间】:2012-02-04 20:21:56 【问题描述】:

我有一个按钮

    <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_
    android:layout_
    android:layout_marginBottom="15dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:text="@string/q1a1"
    android:id="@+id/choice1">
</Button>

当您按下此按钮时,它会为计数器/记分员增加 10。如何使此按钮能够被按下一次,但之后不能再次按下?用户可以作弊并多次按下按钮以增加更多分数。

【问题讨论】:

【参考方案1】:

在您的 onCreate 方法中,这样做:

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

【讨论】:

另一个没用,但这符合我的目的并且效果很好。谢谢大佬!【参考方案2】:

您可以隐藏或禁用它

Button mybutton;

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

【讨论】:

【参考方案3】:

在按下按钮时禁用按钮(在同时处理按钮的“正常”操作的代码中)。

【讨论】:

以上是关于如何使按钮按下一次然后不再可按下?的主要内容,如果未能解决你的问题,请参考以下文章