gpt4 book ai didi

android - 将对话框转换为 AlertDialog

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:30 25 4
gpt4 key购买 nike

我在另一个项目中有完全相同的代码,但它在这里继续崩溃。我的 gradle 中有 implementation 'com.android.support:design:26.1.0'。我真的不确定问题出在哪里。我尝试切换 gradle 版本,从 v7 Dialog 更改为 app Dialog。无论如何都会失败

java.lang.ClassCastException: android.app.Dialog cannot be cast to android.support.v7.app.AlertDialog
at com.example.weather.CreateCityDialog.onResume(CreateCityDialog.java:58)

import android.app.Dialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v4.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CreateCityDialog extends DialogFragment {
public interface NewCityHandler {
void onNewCityCreated(String cityName);
}

private NewCityHandler newCityHandler;
private EditText etName;

@Override
public void onAttach(Context context) {
super.onAttach(context);

if (context instanceof NewCityHandler)
newCityHandler = (NewCityHandler) context;
else
throw new RuntimeException("Error");
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add new city");

View rootView = getActivity().getLayoutInflater().inflate(R.layout.activity_create_city_dialog, null);

etName = rootView.findViewById(R.id.etName);

builder.setView(rootView);

builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});

return super.onCreateDialog(savedInstanceState);
}

@Override
public void onResume() {
super.onResume();
final AlertDialog d = (AlertDialog) getDialog();
if (d != null) {
Button positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!TextUtils.isEmpty(etName.getText())) {
newCityHandler.onNewCityCreated(etName.getText().toString());
d.dismiss();
} else {
etName.setError("Empty field");
}
}
});
}
}
}

最佳答案

您必须从 onCreateDialog() 返回 AlertDialog

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add new city");

View rootView = getActivity().getLayoutInflater().inflate(R.layout.activity_create_city_dialog, null);

etName = rootView.findViewById(R.id.etName);

builder.setView(rootView);

builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}

关于android - 将对话框转换为 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50191440/

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