我在应用程序中有一个带有两个复选框的自定义对话框片段,该片段使用侦听器将值发送到MainActivity。我面临的问题是,当我触摸对话框片段以将其关闭时,该复选框将被取消选中。我需要保持复选框保持其状态。请帮助。
TypeDialogFragment.java
public class TypeDialogFragment extends DialogFragment {
public interface OnInputListener{
void sendInput(String input);
}
private static final String TAG = "TypeDialog";
public OnInputListener mOnInputListener;
CheckBox type1, type2;
View v;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (v != null) {
if ((ViewGroup)v.getParent() != null)
((ViewGroup)v.getParent()).removeView(v);
return v;
}
v = inflater.inflate(R.layout.fragment_type_dialog, container, false);
type1=(CheckBox) v.findViewById(R.id.checkBox2);
type2 =(CheckBox)v.findViewById(R.id.checkBox3) ;
type1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (((CheckBox) view).isChecked()) {
mOnInputListener.sendInput("Type1");
}
}
});
type2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (((CheckBox) view).isChecked()) {
mOnInputListener.sendInput("Type2");
}
}
});
// Do all the stuff to initialize your custom view
// set "origin" to top left corner, so to speak
Window window = getDialog().getWindow();
window.setGravity(Gravity.TOP|Gravity.LEFT);
// after that, setting values for x and y works "naturally"
WindowManager.LayoutParams params = window.getAttributes();
params.x = 20;
params.y = 250;
window.setAttributes(params);
return v;
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.00f;
windowParams.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(windowParams);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mOnInputListener = (OnInputListener) getActivity();
}catch (ClassCastException e){
Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage() );
}
}
}