我正在尝试在不同的片段中重用Dialog。我的应用程序的结构是一个在多个片段之间转换的活动。
我在DialogFragment中定义了一个侦听器接口,然后我在我想要使用它的片段中实现了接口。这是不同的片段显示与正面点击不同的行为。这是DialogFragment
public class AreyouSureDialog extends DialogFragment {
public ConfirmationListener listener;
public interface ConfirmationListener {
public void onPositiveClick(DialogFragment dialog);
public void onNegativeClick(DialogFragment dialog);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.confirmation_dialog, null))
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onPositiveClick(AreyouSureDialog.this);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onNegativeClick(AreyouSureDialog.this);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
listener = (ConfirmationListener) context;
}
}
这是我实现onPositiveClick
public class Profile extends Fragment implements AreyouSureDialog.ConfirmationListener {
SharedPreferences pref;
public static final String pref_filename = "ProfilePreferences";
ImageView profile_pic;
Bitmap bitmap;
String sTargetUri;
Uri targetUri;
public UpdateClickListener listener;
View view;
String name;
String user_name;
String email;
public interface UpdateClickListener {
public void onUpdateClick(String name, String user, String email);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.profile, container, false);
profile_pic = (ImageView) view.findViewById(R.id.img_pic);
final EditText et_name = (EditText) view.findViewById(R.id.et_name);
final EditText et_user = (EditText) view.findViewById(R.id.et_username);
final EditText et_email = (EditText) view.findViewById(R.id.et_email);
EditText et_description = (EditText) view.findViewById(R.id.et_description);
Button btUpdate = (Button) view.findViewById(R.id.bt_update);
//profile_pic.setImageBitmap();
pref = getActivity().getSharedPreferences(pref_filename, 0);
if(!(pref == null))
{
sTargetUri = pref.getString("target_uri", "default");
name = pref.getString("name", "John Doe");
user_name = pref.getString("username", "John Doe");
email = pref.getString("email", "John Doe@gmail.com");
}
if(sTargetUri.equals("default"))
{
profile_pic.setImageResource(R.drawable.ic_menu_camera);
}
else
{
targetUri = Uri.parse(sTargetUri);
try {
bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(targetUri));
profile_pic.setImageBitmap(bitmap);
} catch (FileNotFoundException e){
}
}
et_name.setText(name);
et_user.setText(user_name);
et_email.setText(email);
profile_pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
btUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
String tempName = et_name.getText().toString();
String tempUserName = et_user.getText().toString();
String tempEmail = et_email.getText().toString();
listener.onUpdateClick(tempName, tempUserName, tempEmail);
*/
showDialog();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
targetUri = data.getData();
//Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(targetUri));
SharedPreferences.Editor edit = pref.edit();
edit.putString("target_uri", targetUri.toString());
edit.apply();
profile_pic.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
listener = (UpdateClickListener) context;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Profile");
}
@Override
public void onPositiveClick(DialogFragment dialog) {
System.out.println("hello");
}
@Override
public void onNegativeClick(DialogFragment dialog) {
}
public void showDialog()
{
DialogFragment fragment = new AreyouSureDialog();
fragment.show(getActivity().getSupportFragmentManager(), "dialog");
}
}
当我点击肯定按钮时,我收到以下错误:
java.lang.ClassCastException: com.example.myname.finalapp.MainActivity cannot be cast to com.example.myname.finalapp.AreyouSureDialog$ConfirmationListener
at com.example.myname.finalapp.AreyouSureDialog.onAttach(AreyouSureDialog.java:49)
答案 0 :(得分:1)
片段是不是活动(它们是Context的子类),片段具有相关活动。在onAttach
中,您假设您接收的上下文是ProfileFragment,当它实际上是MainActivity时。
答案 1 :(得分:1)
您将使用您的方法获得对Activity的回调。 Activity需要实现接口。
如果那不是你想要的,你无论如何都在调用构造函数。
new AreyouSureDialog();
所以,添加另一个来接受监听器
public AreyouSureDialog() {}
public AreyouSureDialog(ConfirmationListener listener) {
this.listener = listener;
}
现在你可以使用
了new AreyouSureDialog(Profile.this);
setter方法也可以使用
/* @Override
public void onAttach(Context context) {
super.onAttach(context);
listener = (ConfirmationListener) context;
}
*/
public void setConfirmationListener(ConfirmationListener listener) {
this.listener = listener;
}
答案 2 :(得分:0)
为了澄清,我使用了cricket_007答案,但是我没有传入监听器,而是在MainActivity中传递Fragment
,并在Fragment中实现了onPositiveClick方法,允许我为每个片段定义自定义onClick行为,无需在MainActivity