简体   繁体   English

如何从对话框片段中打开新活动

[英]How to open a new activity from a dialog fragment

My program opens a DialogFragment class while running in MainActivity.java 我的程序在MainActivity.java中运行时打开DialogFragment类

I want to be able to click on the "neutral button" of that dialog and open a new activity, SensorDataDisplay.java 我希望能够单击该对话框的“中性按钮”并打开一个新的活动SensorDataDisplay.java

I am having trouble finding the right way to reference the Context in my button's onClick. 我无法在按钮的onClick中找到引用上下文的正确方法。

package com.august.customtisensortagclient;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import java.util.ArrayList;

public class GetInfoDialog extends DialogFragment {


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {





        final String thisWhichGetInfoDialog = ((MainActivity)getActivity()).getWhichGetInfoDialog();
        final ArrayList<String> thisScannedDevicesArrayList =
                ((MainActivity)getActivity()).getScannedDevicesArrayList();
        final int thisIsInLeftConnectedDeviceDisplay = ((MainActivity)getActivity()).getIsInLeftConnectedDeviceDisplay();
        final int thisIsInRightConnectedDeviceDisplay = ((MainActivity)getActivity()).getIsInRightConnectedDeviceDisplay();
        int thisIsInThisConnectedDeviceDisplay = 0;

        if (thisWhichGetInfoDialog == "Left") {
            thisIsInThisConnectedDeviceDisplay = thisIsInLeftConnectedDeviceDisplay;
        } else if (thisWhichGetInfoDialog == "Right")
            thisIsInThisConnectedDeviceDisplay = thisIsInRightConnectedDeviceDisplay;

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(thisWhichGetInfoDialog + " Sensor Info");
        builder.setMessage("MAC Address: " + thisScannedDevicesArrayList.get(thisIsInThisConnectedDeviceDisplay));
        builder.setNeutralButton("View Data", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent myIntent = new Intent(?????, SensorDataDisplay.class);
                myIntent.putExtra("key", "TEST VALUE"); //Optional parameters
                ?????.startActivity(myIntent);
            }
        });
        builder.setNegativeButton("Done", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User cancelled the dialog
            }
        });
        return builder.create();
    }
}

DialogFragment has getActivity() and getContext() methods (which it inherits from Fragment ), both will work in your case. DialogFragment具有getActivity()getContext()方法(它们是从Fragment继承的),在您的情况下都可以使用。 If you're having trouble accessing these methods from the anonymous class (which shouldn't be the case), you can use the GetInfoDialog.this.getActivity() syntax. 如果您在从匿名类访问这些方法时遇到麻烦(不应该这样),则可以使用GetInfoDialog.this.getActivity()语法。

getActivity() returns the Activity the fragment is attached to getActivity()返回片段附加到的Activity

builder.setNeutralButton("View Data", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent myIntent = new Intent(getActivity(), SensorDataDisplay.class);
                    myIntent.putExtra("key", "TEST VALUE"); //Optional parameters
                    getActivity().startActivity(myIntent);
                }
            });

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

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