Android Paypal启动按钮OnClick仅在活动生命周期中执行一次

时间:2012-03-13 20:27:43

标签: android paypal onclick onclicklistener

这一个正在摧毁我的大脑,当你进入活动时,你可以点击paypal按钮,你就可以进入贝宝活动了。如果您取消PayPal活动,您将被带回原始活动。从这里再次单击按钮没有任何反应。我尝试在OnClick方法中将基本消息打印到控制台,它甚至没有显示,它似乎没有注册点击。我假设问题与布局xml文件或清单文件无关。我会发布一张图片,但我不能因为我是新的,所有你需要知道的是它的结账屏幕真的有支付宝按钮。

提前致谢,             休

package com.cit.datastructuretesting;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.paypal.android.MEP.CheckoutButton;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalPayment;

public class StoreItemInterface extends Activity implements OnClickListener, OnKeyListener
{           
Double subtotal, shipping, total;
EditText etItemQuantity;
TextView tvItemSubtotal, tvItemTotal;
DecimalFormat decFormat = new DecimalFormat("#.##");
PayPal ppObj;
CheckoutButton launchPayPalButton;



@Override
protected void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);     

    setContentView(R.layout.storeiteminterface);

    ImageView ivItemImage = (ImageView) findViewById(R.id.ivItemImage2);
    TextView tvItemTitle = (TextView) findViewById(R.id.tvItemTitle2);
    TextView tvItemDescription = (TextView) findViewById(R.id.tvItemDescription2);
    etItemQuantity = (EditText) findViewById(R.id.etItemQuantity);
    tvItemSubtotal = (TextView) findViewById(R.id.tvItemSubtotal);
    TextView tvItemShipping = (TextView) findViewById(R.id.tvItemShipping);
    tvItemTotal = (TextView) findViewById(R.id.tvItemTotal);    

    subtotal = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getPrice() * Integer.parseInt(etItemQuantity.getText().toString());
    shipping = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getShippingPrice();
    total = subtotal + shipping;

    ivItemImage.setImageBitmap(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getImageBitmap());            
    tvItemTitle.setText(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getTitle());
    tvItemDescription.setText(Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getDescription());
    tvItemSubtotal.setText("€" + decFormat.format(subtotal));       
    tvItemShipping.setText("€" + decFormat.format(shipping));       
    tvItemTotal.setText("€" + decFormat.format(total)); 

    etItemQuantity.setOnKeyListener(this);

    //setup paypal stuff
    ppObj = PayPal.initWithAppID(this.getBaseContext(), "APP-80W284485P519543T", PayPal.ENV_SANDBOX);

    launchPayPalButton = ppObj.getCheckoutButton(this, PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.bottomMargin = 10;

    launchPayPalButton.setLayoutParams(params);        

    ((RelativeLayout)findViewById(R.id.paypalLayout)).addView(launchPayPalButton); 

    launchPayPalButton.setOnClickListener(this);
}

@Override
public void onClick(View arg0) 
{       
    if(arg0.getId() == launchPayPalButton.getId())
    {
        System.out.println("TEST");

        if(!tvItemTotal.getText().toString().equals("---"))
        {
            PayPalPayment newPayment = new PayPalPayment();
            newPayment.setSubtotal(BigDecimal.valueOf(total));
            newPayment.setCurrencyType("EUR");
            newPayment.setRecipient("Cape_1328492032_biz@mycit.ie");
            newPayment.setMerchantName("Cape Clear App");

            Intent paypalIntent = PayPal.getInstance().checkout(newPayment, StoreItemInterface.this);
            StoreItemInterface.this.startActivityForResult(paypalIntent, 1);
        }
        else
        {
            Toast.makeText(StoreItemInterface.this, "Invalid Quantity!", Toast.LENGTH_SHORT).show();
        }
    }       
}

@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) 
{       
    if(etItemQuantity.getText().toString() != null && !etItemQuantity.getText().toString().trim().equals("") && !etItemQuantity.getText().toString().trim().equals("0"))
    {
        subtotal = Main.appData.getStore().getStoreCatagory(StoreInterface.currentCatagory).getStoreItem(StoreItemsInterface.currentItem).getPrice() * Integer.parseInt(etItemQuantity.getText().toString());
        total = subtotal + shipping;

        tvItemSubtotal.setText("€" + subtotal);         
        tvItemTotal.setText("€" + decFormat.format(total));
    }
    else
    {
        tvItemSubtotal.setText("---");          
        tvItemTotal.setText("---");         
    }

    return false;
}   

}

2 个答案:

答案 0 :(得分:5)

答案 1 :(得分:3)

如果您仍在寻找答案,我有一个

如果您查看getCheckoutButton方法,则需要Context作为参数,因此当Activity用于说当你开始另一个Activity时会发生暂停,CheckoutButton的实例会以某种方式丢失。

我修复了在Activity的onResume中使用updateButton方法

@Override
protected void onResume() {
    /**
     * The CheckoutButton has to be updated each time the Activity is
     * resumed, otherwise the onClickListener of CheckoutButton will not work
     **/
    if (mCheckOutBtn != null && (mCheckOutBtn instanceof CheckoutButton))
        mCheckOutBtn.updateButton();
    super.onResume();
}

考虑到您在PayPal的{​​{1}}中初始化了CheckoutButton库和onCreate,这是有效的。