有没有办法从MainActivity访问Dialog对象?

时间:2019-06-27 01:57:41

标签: android dialog

我想在MainActivity中访问Dialog的对象。 例如,在MainActivity中,使用“按钮”弹出一个对话框。

case R.id.txtCalibMain:
    SubMenu_Calib mCalibDialog = new SubMenu_Calib(instance);
    mCalibDialog.show();
    break;

以这种方式,但是在前面的步骤中,在对话框之前 如果要在对话框中保留TextView的文本内容 您怎么办呢?

当前,使用单独的类定义和调用Dialog。 我试图从外部访问它,所以我要导入一个空值或一个未定义的TextView。 很自然会引发错误。

对话框活动代码

public class SubMenu_Units extends Dialog implements View.OnClickListener {

    public static SubMenu_Units mSubMenu_Units;
    public static MainActivity instance;
    public static List<SingleItem> singleItems;
    static SingleAdapter mSingleAdapter;
    public static TextView mtxtTestMenu;

    public SubMenu_Units(Context context) {
        super(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.submenu_units_layout);

        mSubMenu_Units = this;

        initialize();
        mtxtTestMenu = (TextView)findViewById(R.id.txtTestCenterMenu);
    }

    void initialize()
    {
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        setCanceledOnTouchOutside(true);

        ColorDrawable dialogColor = new ColorDrawable(0xFF424957); // 0xFF424957
        getWindow().setBackgroundDrawable(dialogColor);

        mOrientation = getContext().getResources().getConfiguration().orientation;

            WindowManager.LayoutParams mPortrait_params = getWindow().getAttributes();
            mPortrait_params.width = 375;
            mPortrait_params.height = 945;
            mPortrait_params.gravity = Gravity.CENTER | Gravity.LEFT;
            mPortrait_params.x = 5;
            mPortrait_params.y = 68;
            getWindow().setAttributes(mPortrait_params);
    }

1 个答案:

答案 0 :(得分:0)

您需要具有公共类,需要在其中复制以下代码

即您需要使用CommonActivity在CurrentActivity下面进行扩展

//在当前活动中

  case R.id.txtCalibMain:
       exitAlert("Some Message)
        break;

//主要活动

public void exitAlert(final String message) {

            LayoutInflater inflater = LayoutInflater.from(this);
            View mExitAlertView = inflater.inflate(R.layout.alert_exit_logout_dialog, null);
            final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(mExitAlertView);
            dialog.setCanceledOnTouchOutside(true);

            TextView txtAlertMessage = (TextView) mExitAlertView.findViewById(R.id.txtAlertMessage);
            txtAlertMessage.setText(message);

            mExitAlertView.setOnTouchListener((v, event) -> {
                dialog.dismiss();
                return false;
            });

            Button btnExitAlertNo = (Button) mExitAlertView.findViewById(R.id.btnExitAlertNo);
            btnExitAlertNo.setOnClickListener(v -> dialog.dismiss());

            Button btnExitAlertYes = (Button) mExitAlertView.findViewById(R.id.btnExitAlertYes);
            btnExitAlertYes.setOnClickListener(v -> {

                dialog.dismiss();
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);
            });

            dialog.show();
        }