我正在使用像这样的DialogFragment,
public class Dfragment extends DialogFragment {
public Dfragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
ab.setMessage("Delete Everything").setTitle("DELETE DB")
.setPositiveButton("OK",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("CANCEL",new DialogInterface.OnClickListener()
{ @Override
public void onClick(DialogInterface dialog, int which) { }
});
return ab.create();
}
}
我可以从片段中检测到正/负,但是如何从调用它的Activity中检测到它。
以下是Activity中实现的代码,
Dfragment frag = new Dfragment();
frag.show(getFragmentManager(),"THISDIALOG");
所以在Activity中如何确定单击了哪个按钮?
到目前为止我尝试过的事情,
从上面的片段代码中,
在上面的代码中替换了setPositiveButton(“OK”,null),但仍无法正常工作。
我需要修改哪些代码才能使其按预期工作?
编辑1 :
我知道它可以通过接口实现,但我想在没有它的情况下实现它。
答案 0 :(得分:3)
我有一个建议给你。你可以这样做:
public abstract class Dfragment extends DialogFragment {
public Dfragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
ab.setMessage("Delete Everything").setTitle("DELETE DB")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onPositiveButtonClick();
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
onNegativeButtonClick();
}
});
return ab.create();
}
public abstract void onPositiveButtonClick();
public abstract void onNegativeButtonClick();
}
创建对象
Dfragment frag = new Dfragment() {
@Override
public void onPositiveButtonClick() {
}
@Override
public void onNegativeButtonClick() {
}
};
frag.show(getFragmentManager(),"THISDIALOG");
答案 1 :(得分:0)
您无需从活动中检测到它。 只需在侦听器中编写您的代码,并按照您调用的方式调用。例如......
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
ab.setMessage("Delete Everything").setTitle("DELETE DB")
.setPositiveButton("OK",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("log", "this will log anyway");
}
})
.setNegativeButton("CANCEL",new DialogInterface.OnClickListener()
{ @Override
public void onClick(DialogInterface dialog, int which) { }
});
return ab.create();
}
除此之外,如果你想要做任何其他事情而不是你需要实现callback
答案 2 :(得分:0)
使用界面,
public class Dfragment extends DialogFragment {
Clicks objclick;
public Dfragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
ab.setMessage("Delete Everything").setTitle("DELETE DB")
.setPositiveButton("OK",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
obj.onclick();
}
})
.setNegativeButton("CANCEL",new DialogInterface.OnClickListener()
{ @Override
public void onClick(DialogInterface dialog, int which) { }
});
return ab.create();
}
public void clicklistner(Clicks click){
objclick=click;
}
}
制作界面,
public interface Clicks{
public void onclick();
}
从您的活动中调用clicklistner方法,
frag.clicklistner(new....