单击时为什么没有调用OnClickListner?

时间:2015-07-05 12:20:09

标签: android oop methods

这是我的BASE类的代码,我将这个类继承到另一个子类。从那个类我调用这个函数ActiveSMSPackage()。我的代码是完美的,没有任何错误但是方法没有被调用。你能告诉我我在哪里做错了吗?

public class PrepaidSMSBase extends Activity {

private String smsNumber = "";
private String smsPackageName;
private String smsPrice;
private String smsTitle;
private String smsText;


public PrepaidSMSBase(){}

public void setSmsPackageInformation(String smsTitle, String smsPackageName, String smsPrice, String smsNumber, String smsText)
{
    this.smsTitle = smsTitle;
    this.smsPackageName = smsPackageName;
    this.smsPrice = smsPrice;
    this.smsNumber = smsNumber;
    this.smsText = smsText;

}

public void activeSMSPackage()
{
    try
    {AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidSMSBase.this);
        builder.setTitle(smsTitle);
        builder.setMessage("Are you sure you want to active" + PrepaidSMSBase.this.smsPackageName + "in RS: " +
                PrepaidSMSBase.this.smsPrice);
        builder.setPositiveButton("Activate", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Uri uri = Uri.parse("smsto:" + smsNumber);
                Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                intent.putExtra("sms_body", smsText);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "Please click send button to activate desire Package", Toast.LENGTH_LONG).show();
            }
        });
    }catch (ActivityNotFoundException e){
        e.printStackTrace();
    }
}

儿童类代码是:

public class SMSCheckClass extends PrepaidSMSBase implements View.OnClickListener{

Button checkButton;

public SMSCheckClass(){
    setSmsPackageInformation("Test 1","Some Thing","50","660","Sub");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.checksms);
    checkButton = (Button)findViewById(R.id.chkBtn);
    checkButton.setOnClickListener(this);
}

@Override
public void onClick(View v)
{
    activeSMSPackage();
}

2 个答案:

答案 0 :(得分:1)

首先,您应该在activeSMSPackage中显示对话框,请在该方法中添加此代码:

builder.show()

答案 1 :(得分:1)

使用此代码

        {AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidSMSBase.this);
        builder.setTitle(smsTitle);
        builder.setMessage("Are you sure you want to active" + PrepaidSMSBase.this.smsPackageName + "in RS: " +
                PrepaidSMSBase.this.smsPrice);
        builder.setPositiveButton("Activate", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Uri uri = Uri.parse("smsto:" + smsNumber);
                Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                intent.putExtra("sms_body", smsText);
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "Please click send button to activate " + PrepaidSMSBase.this.smsPackageName, Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.show();

您只需在builder.show()的末尾添加AlertDialog,因为您的函数是在AlertDialog内写的,而show()方法用于显示AlertDialog }