从一个主类继承类

时间:2015-06-22 12:16:38

标签: java android inheritance

我是编程新手,我在inheritance没有太多经验。现在我有一天在Android开发工作。这是我的班级代码。我想继承这个类的所有类。只有差异是继承类应该在AlertDialog方法中更改call()消息和拨号号码。请告诉我如何继承这个类到另一个代码应该是什么?

public class PrepaidChineTown extends AppCompatActivity {

Button chinaButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.prepaidchinatown);
    chinaButton = (Button)findViewById(R.id.chinaTownBtn);
    chinaButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder builder =  new AlertDialog.Builder(PrepaidChineTown.this);
            builder.setTitle("China Town Offer");
            builder.setMessage("Are you sure you want to activate China Town Offer? Terms and condition apply");
            builder.setCancelable(false);
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    call();
                }


            });

            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {dialog.cancel();

                }
            });
            AlertDialog alert = builder.create();
            alert.show();

        }

    });

}

public void call(){

    Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode("*2300#")));
    try{
        startActivity(in);
    }catch (ActivityNotFoundException e){
        Toast.makeText(getApplicationContext(), "Your Activity is not Found", Toast.LENGTH_LONG).show();
    }
}
}

2 个答案:

答案 0 :(得分:1)

创建Constant.class

  private String number  = "";

    public class Constant {

          public static void alertDialogShow(Context context,String title,String message,String _number)
                {
this.number = _number;

                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle(title);
                    builder.setMessage(message);
                    builder.setCancelable(false);
                    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
        //to do your stuff
                        }

                    });

                    builder.setNegativeButton("No", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            dialog.cancel();

                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show();

                }

    public void call(){

        Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode(number)));
        try{
            startActivity(in);
        }catch (ActivityNotFoundException e){
            Toast.makeText(getApplicationContext(), "Your Activity is not Found", Toast.LENGTH_LONG).show();
        }
    }

        }

你可以在任何活动中调用它。 像这样。

Constant.alertDialogShow(CurrentActivity.this,"Title","Message","*2300#");

答案 1 :(得分:0)

    public class PrepaidChineTown extends AppCompatActivity implements OnClickListener
    { 

         Button chinaButton;

         @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prepaidchinatown);
        chinaButton = (Button)findViewById(R.id.chinaTownBtn);
        chinaButton.setOnClickListener(this);

    }

public void alertDialogue(String title,String message,Context context)
    {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setCancelable(false);
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                call();
            }

        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.cancel();

            }
        });
        AlertDialog alert = builder.create();
        alert.show();

    }

    public void call(){

        Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode("*2300#")));
        try{
            startActivity(in);
        }catch (ActivityNotFoundException e){
            Toast.makeText(getApplicationContext(), "Your Activity is not Found", Toast.LENGTH_LONG).show();
        }
    }

现在创建PrepaidChineTown的对象并通过传递所需参数来调用alertDialogue(),或者通过扩展此类,您可以使用该方法来显示对话