繁体   English   中英

从一个主类继承类

[英]Inheriting classes from one main class

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

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();
    }
}
}

创建一个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#");
    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() 您可以使用该方法来显示对话

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM