首页 > java > 如何从扩展活动覆盖方法?

如何从扩展活动覆盖方法? (How to override method from extend activity?)

问题

我有2项活动。一个是AlertDialogActivity第二个是MainActivity。我延伸AlertDialogActivity进去MainActivity

像这样 public class MainActivity extends AlertDialogActivity

现在如何覆盖AlertDialogActivity我的方法MainActivity

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


    }

}

解决方法

如果您希望其他类能够调用它,请showAlertDialog protected改为代替private,甚至公开。

protected void showAlertDialog(final Context context) {

}

私有方法无法覆盖。

问题

I have 2 activity. One is AlertDialogActivity & second is MainActivity. I extend AlertDialogActivity in MainActivity.

like this public class MainActivity extends AlertDialogActivity

now how to override method which is in AlertDialogActivity to my MainActivity?

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.

protected void showAlertDialog(final Context context) {

}

Private methods can not be overriden.

相似信息