如何将数据从 Activity 传递到 Android Studio 中的对话框
How pass data from Activity to Dialog in Android Studio
我需要将数据从 MainActivity 传递到 Android Studio 中的对话框,以便在启动 AlertDialog 时,我从 MainActivity 发送的值可以显示在其中。到目前为止,使用这段代码我只能设法将数据从 Dialog 传递到 MainActivity 而不是以相反的方式
我要发送的数据是:
IPAdreess = "192.1681.1.4";
IPPort ="8089";
这是我的 MainActivity:
public class MainActivity extends AppCompatActivity implements ConfigWIFIDialog.ConfigWIFIDialogListener {
private String IPAdreess = "192.1681.1.4";
private String IPPort ="8089";
private TextView myIPAdreess;
private TextView myIPPort;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myIPAdreess = (TextView) findViewById(R.id.txv_ipAddress);
myIPPort = (TextView) findViewById(R.id.txv_ipPort);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});
}
public void openDialog() {
ConfigWIFIDialog configWIFIDialog = new ConfigWIFIDialog();
configWIFIDialog.show(getSupportFragmentManager(), "example dialog");
}
@Override
public void applyTexts(String mIpAddreess, String mIpPort) {
myIPAdreess.setText(mIpAddreess);
myIPPort.setText(mIpPort);
}
}
这是对话框:
public class ConfigWIFIDialog extends AppCompatDialogFragment {
private EditText myIpAddress;
private EditText myIpPort;
private ConfigWIFIDialogListener listener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.config_wifi_dialog, null);
myIpAddress = (EditText) view.findViewById(R.id.etx_ipAddress);
myIpPort = view.findViewById(R.id.etx_ipPort);
builder.setView(view)
.setTitle("Configuracion WIFI")
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String mIpAddreess = myIpAddress.getText().toString();
String mIpPort = myIpPort.getText().toString();
listener.applyTexts(mIpAddreess, mIpPort);
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (ConfigWIFIDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
"must implement ConfigWIFIDialogListener");
}
}
public interface ConfigWIFIDialogListener {
void applyTexts(String mIpAddreess, String mIpPort);
}
}
您正在实现 AppCompatDialogFragment,您可以通过设置其 arguments
来向其传递数据
public void openDialog() {
ConfigWIFIDialog configWIFIDialog = new ConfigWIFIDialog();
Bundle args = new Bundle();
args.putString("title", title);
configWIFIDialog.setArguments(args);
configWIFIDialog.show(getSupportFragmentManager(), "example dialog");
}
您可以从 codepath
了解更多
我需要将数据从 MainActivity 传递到 Android Studio 中的对话框,以便在启动 AlertDialog 时,我从 MainActivity 发送的值可以显示在其中。到目前为止,使用这段代码我只能设法将数据从 Dialog 传递到 MainActivity 而不是以相反的方式 我要发送的数据是:
IPAdreess = "192.1681.1.4";
IPPort ="8089";
这是我的 MainActivity:
public class MainActivity extends AppCompatActivity implements ConfigWIFIDialog.ConfigWIFIDialogListener {
private String IPAdreess = "192.1681.1.4";
private String IPPort ="8089";
private TextView myIPAdreess;
private TextView myIPPort;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myIPAdreess = (TextView) findViewById(R.id.txv_ipAddress);
myIPPort = (TextView) findViewById(R.id.txv_ipPort);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});
}
public void openDialog() {
ConfigWIFIDialog configWIFIDialog = new ConfigWIFIDialog();
configWIFIDialog.show(getSupportFragmentManager(), "example dialog");
}
@Override
public void applyTexts(String mIpAddreess, String mIpPort) {
myIPAdreess.setText(mIpAddreess);
myIPPort.setText(mIpPort);
}
}
这是对话框:
public class ConfigWIFIDialog extends AppCompatDialogFragment {
private EditText myIpAddress;
private EditText myIpPort;
private ConfigWIFIDialogListener listener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.config_wifi_dialog, null);
myIpAddress = (EditText) view.findViewById(R.id.etx_ipAddress);
myIpPort = view.findViewById(R.id.etx_ipPort);
builder.setView(view)
.setTitle("Configuracion WIFI")
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String mIpAddreess = myIpAddress.getText().toString();
String mIpPort = myIpPort.getText().toString();
listener.applyTexts(mIpAddreess, mIpPort);
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (ConfigWIFIDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
"must implement ConfigWIFIDialogListener");
}
}
public interface ConfigWIFIDialogListener {
void applyTexts(String mIpAddreess, String mIpPort);
}
}
您正在实现 AppCompatDialogFragment,您可以通过设置其 arguments
public void openDialog() {
ConfigWIFIDialog configWIFIDialog = new ConfigWIFIDialog();
Bundle args = new Bundle();
args.putString("title", title);
configWIFIDialog.setArguments(args);
configWIFIDialog.show(getSupportFragmentManager(), "example dialog");
}
您可以从 codepath
了解更多