gpt4 book ai didi

java - 如何从对话框 fragment 打开新 Activity

转载 作者:行者123 更新时间:2023-12-01 19:47:53 25 4
gpt4 key购买 nike

我的程序在 MainActivity.java 中运行时打开 DialogFragment 类

我希望能够单击该对话框的“中立按钮”并打开一个新 Activity SensorDataDisplay.java

我无法找到在按钮的 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();
}
}

最佳答案

DialogFragmentgetActivity()getContext() 方法(它继承自 Fragment),两者都有适合你的情况。如果您在从匿名类访问这些方法时遇到问题(不应该出现这种情况),您可以使用 GetInfoDialog.this.getActivity() 语法。

关于java - 如何从对话框 fragment 打开新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52462725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com