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

我有 2 个活动。一是AlertDialogActivity& 二是MainActivity。我AlertDialogActivity在MainActivity.


像这样public class MainActivity extends AlertDialogActivity


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


警报对话活动:


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



    }


}


梦里花落0921
浏览 137回答 3
3回答

莫回无

如果您希望其他类能够调用它,请将其showAlertDialog protected改为,甚至公开。privateprotected void showAlertDialog(final Context context) {}私有方法不能被覆盖。

慕工程0101907

如果你想覆盖一个方法使用 抽象 关键字public abstract class AlertDialogActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_alert_dialog);&nbsp; &nbsp; showAlertDialog(AlertDialogActivity.this);}&nbsp; public abstract void showAlertDialog(final Context context) {&nbsp; &nbsp; // 1. Instantiate an <code><a href="/reference/android/app/AlertDialog.Builder.html">AlertDialog.Builder</a></code> with its constructor&nbsp; &nbsp; AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogActivity.this);&nbsp; // 2. Chain together various setter methods to set the dialog characteristics&nbsp; &nbsp; builder.setMessage("How are you?")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setTitle("Hello");&nbsp; &nbsp;// 3. Get the <code><a&nbsp;&nbsp; href="/reference/android/app/AlertDialog.html">AlertDialog</a></code> from <code><a&nbsp;&nbsp; href="/reference/android/app/AlertDialog.Builder.html#create()">create()</a></code>&nbsp; &nbsp; AlertDialog dialog = builder.create();&nbsp; &nbsp; dialog.show();}

largeQ

方法应该具有相同的名称和类型。方法也不应该是私有的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java