我正在尝试为我的应用制作捐款菜单。我已经找到了当用户点击捐赠时,会出现更多按钮的部分。现在,如果他们再次单击相同的按钮,我希望能够让金额按钮消失。我想要保留常规的DonateButton。我怎么会这样做?
我已使用purchaseButton.setVisibility(View.GONE);
以下是单击按钮和其他按钮的代码:
public void onClick(View v) {
switch (v.getId()) {
case R.id.DonateButton:
purchaseButton.setVisibility(View.VISIBLE);
purchaseButton2.setVisibility(View.VISIBLE);
purchaseButton3.setVisibility(View.VISIBLE);
case R.id.Donate:
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "donate");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i(TAG,"Can't purchase on this device");
}
break;
default:
// nada
Log.i(TAG,"default. ID: "+v.getId());
break;
case R.id.Donatetwo:
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "donate2");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i(TAG,"Can't purchase on this device");
}
break;
case R.id.Donatethree:
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "donate3");
// android.test.purchased or android.test.canceled or android.test.refunded
} else {
Log.i(TAG,"Can't purchase on this device");
}
break;
}
}
我的意思截图:
图片网址(不要有10个声明): http://i.stack.imgur.com/AMdhS.png
我想说的是。
我希望它仍允许它们多次打开和关闭这些按钮。
答案 0 :(得分:1)
一个简单的状态变量应该这样做。
将其放在您的字段定义区域中:
boolean areButtonAmountVisible = false;
此代码为onClick()
:
case R.id.DonateButton:
if( areButtonAmountVisible )
{
areButtonAmountVisible = false;
purchaseButton.setVisibility(View.GONE);
purchaseButton2.setVisibility(View.GONE);
purchaseButton3.setVisibility(View.FONE);
}
else
{
areButtonAmountVisible = true;
purchaseButton.setVisibility(View.VISIBLE);
purchaseButton2.setVisibility(View.VISIBLE);
purchaseButton3.setVisibility(View.VISIBLE);
}
答案 1 :(得分:1)
试试这个。
使用getVisibility()
方法了解按钮的可见状态。
int visibility;
visibility = button.getVisibility();
if(visibility == View.VISIBLE) {
button.setVisibility(View.INVISIBLE);
} else {
button.setVisibility(View.VISIBLE);
}
你必须为每个按钮检查一次。
我会再提出一件事。
获取全局变量并将其用作通知程序,在所有按钮单击功能中使用以下代码。
boolean again = false;
if (again) {
// make all invisible
again = false;
} else {
// make all visible
again = true;
}