我有2个活动。一个是AlertDialogActivity,第二个是MainActivity。我在AlertDialogActivity中扩展MainActivity

像这样public class MainActivity extends AlertDialogActivity

现在如何覆盖我的AlertDialogActivityMainActivity中的方法?

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) {

}


私有方法不能被覆盖。

10-04 11:31