简体   繁体   English

如何从扩展活动中覆盖方法?

[英]How to override method from extend activity?

I have 2 activity. 我有2个活动。 One is AlertDialogActivity & second is MainActivity . 一个是AlertDialogActivity ,第二个是MainActivity I extend AlertDialogActivity in MainActivity . 我在MainActivity扩展AlertDialogActivity

like this public class MainActivity extends AlertDialogActivity 像这样的public class MainActivity extends AlertDialogActivity

now how to override method which is in AlertDialogActivity to my MainActivity ? 现在如何将AlertDialogActivity方法覆盖到我的MainActivity

AlertDialogActivity: AlertDialogActivity:

package com.jimmytrivedi.alertdialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class AlertDialogActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);
        showAlertDialog(AlertDialogActivity.this);
    }

    private void showAlertDialog(final Context context) {
        // 1. Instantiate an <code><a href="/reference/android/app/AlertDialog.Builder.html">AlertDialog.Builder</a></code> with its constructor
        AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);

// 2. Chain together various setter methods to set the dialog characteristics
        builder.setMessage("How are you?")
                .setTitle("Hello");

// 3. Get the <code><a href="/reference/android/app/AlertDialog.html">AlertDialog</a></code> from <code><a href="/reference/android/app/AlertDialog.Builder.html#create()">create()</a></code>
        AlertDialog dialog = builder.create();
        dialog.show();


    }

}

Make the showAlertDialog protected instead of private , or even public if you want other classes to be able to call it. 如果希望其他类能够调用showAlertDialogshowAlertDialog protected而不是private ,甚至是public。

protected void showAlertDialog(final Context context) {

}

Private methods can not be overriden. 私有方法不能被覆盖。

if you want to override a method Use abstract keyword 如果要覆盖方法,请使用abstract关键字

public abstract class AlertDialogActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alert_dialog);
    showAlertDialog(AlertDialogActivity.this);
}

  public abstract void showAlertDialog(final Context context) {

    // 1. Instantiate an <code><a href="/reference/android/app/AlertDialog.Builder.html">AlertDialog.Builder</a></code> with its constructor
    AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);

  // 2. Chain together various setter methods to set the dialog characteristics
    builder.setMessage("How are you?")
            .setTitle("Hello");

   // 3. Get the <code><a 
  href="/reference/android/app/AlertDialog.html">AlertDialog</a></code> from <code><a 
  href="/reference/android/app/AlertDialog.Builder.html#create()">create()</a></code>
    AlertDialog dialog = builder.create();
    dialog.show();


}

Method should be with the same name and type. 方法应具有相同的名称和类型。 Method should also not be private . 方法也不应该是私有的

It can be protected, package-private or public . 它可以是受保护的,打包私有的或public的 More about that you can find here: What is the difference between public, protected, package-private and private in Java? 您可以在这里找到更多关于此的信息: Java中public,protected,package-private和private之间有什么区别?

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

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