多个 DialogFragment 事件回调 - 我如何知道要更新哪个片段?
Multiple DialogFragment event callbacks - how do I tell which fragment to update?
这是我的情况。我有一个片段,上面有两个按钮。当您点击任一按钮时,会出现一个 DialogFragment
,其中包含一个 EditText
和 ok/cancel 按钮。两个按钮打开相同的 DialogFragment
,但输入 EditText
的数据需要分开。
我最近开始从 Android 文档 seen here 中实现片段事件回调模式,但是 运行 遇到了一个问题 - 我有两个按钮使用相同的事件回调并且我不确定如何区分用户刚刚使用完哪个。因此,以文档为例,我可以从同一屏幕上的两个按钮打开 FragmentA
,但需要根据我单击的按钮不同地处理结果。
在我的片段中:
public static class FragmentA extends DialogFragment {
public interface OnEditNameListener {
public void onPositiveButtonClicked(String newName);
}
}
@Override
public void onAttach(Context context){
super.onAttach(context);
try {
mListener = (OnEditNameListener ) context;
}
catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnEditNameListener ");
}
}
在我的 Activity 中,它实现了 OnEditNameListener:
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
(new EditNameDialog.Builder())
.setTitle(getContext().getString(R.string.title))
.setValue(currentText)
.show(mParentFragmentActivity.getSupportFragmentManager());
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
(new EditNameDialog.Builder())
.setTitle(getContext().getString(R.string.title2))
.setValue(currentText2)
.setInputType(InputType.TYPE_CLASS_NUMBER)
.show(mParentFragmentActivity.getSupportFragmentManager());
}
});
@Override
public void onPositiveButtonClicked(String newName) {
... //does stuff with the name.
//Currently no way to determine whether this came from button1 or button2.
}
目前,两个回调都使用来自 DialogFragment
的输入点击相同的 OnPositiveButtonClicked 方法,但我不知道如何确定这两个按钮中的哪一个来自。
首先,您必须向 onPositiveButtonClicked(String name,int buttonId)
添加一个参数,然后根据按下的按钮向 FragmentA 传递一个参数,例如:
FragmentA fragment=new FragmentA();
Bundle args = new Bundle();
args.putInt("buttonId", 1 or 2);
fragment.setArguments(args);
//open the fragment from the activity
然后在你的 FragmentA onCreate
方法中尝试:
int buttonId = getArguments().getInt("buttonId");
最后当按下肯定按钮时调用:
onPositiveButtonClicked(newName,buttonId)
更新
一个更好的解决方案是在您的 DialogFragment 中创建一个 setter 并使用像这样的匿名接口:
(new EditNameDialog.Builder())
.setTitle(getContext().getString(R.string.title))
.setValue(currentText).setOnEditNameListener(new EditNameListener{
@Override
onPositiveButtonClicked(String newName){
//handle action
}
});
然后在您的 DialogFragment 中添加 setter:
EditNameListener listener;
public DialogFragment setOnEditNameListener(EditNameListener listener){
this.listener=listener;
return this;
}
这是我的情况。我有一个片段,上面有两个按钮。当您点击任一按钮时,会出现一个 DialogFragment
,其中包含一个 EditText
和 ok/cancel 按钮。两个按钮打开相同的 DialogFragment
,但输入 EditText
的数据需要分开。
我最近开始从 Android 文档 seen here 中实现片段事件回调模式,但是 运行 遇到了一个问题 - 我有两个按钮使用相同的事件回调并且我不确定如何区分用户刚刚使用完哪个。因此,以文档为例,我可以从同一屏幕上的两个按钮打开 FragmentA
,但需要根据我单击的按钮不同地处理结果。
在我的片段中:
public static class FragmentA extends DialogFragment {
public interface OnEditNameListener {
public void onPositiveButtonClicked(String newName);
}
}
@Override
public void onAttach(Context context){
super.onAttach(context);
try {
mListener = (OnEditNameListener ) context;
}
catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnEditNameListener ");
}
}
在我的 Activity 中,它实现了 OnEditNameListener:
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
(new EditNameDialog.Builder())
.setTitle(getContext().getString(R.string.title))
.setValue(currentText)
.show(mParentFragmentActivity.getSupportFragmentManager());
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
(new EditNameDialog.Builder())
.setTitle(getContext().getString(R.string.title2))
.setValue(currentText2)
.setInputType(InputType.TYPE_CLASS_NUMBER)
.show(mParentFragmentActivity.getSupportFragmentManager());
}
});
@Override
public void onPositiveButtonClicked(String newName) {
... //does stuff with the name.
//Currently no way to determine whether this came from button1 or button2.
}
目前,两个回调都使用来自 DialogFragment
的输入点击相同的 OnPositiveButtonClicked 方法,但我不知道如何确定这两个按钮中的哪一个来自。
首先,您必须向 onPositiveButtonClicked(String name,int buttonId)
添加一个参数,然后根据按下的按钮向 FragmentA 传递一个参数,例如:
FragmentA fragment=new FragmentA();
Bundle args = new Bundle();
args.putInt("buttonId", 1 or 2);
fragment.setArguments(args);
//open the fragment from the activity
然后在你的 FragmentA onCreate
方法中尝试:
int buttonId = getArguments().getInt("buttonId");
最后当按下肯定按钮时调用:
onPositiveButtonClicked(newName,buttonId)
更新
一个更好的解决方案是在您的 DialogFragment 中创建一个 setter 并使用像这样的匿名接口:
(new EditNameDialog.Builder())
.setTitle(getContext().getString(R.string.title))
.setValue(currentText).setOnEditNameListener(new EditNameListener{
@Override
onPositiveButtonClicked(String newName){
//handle action
}
});
然后在您的 DialogFragment 中添加 setter:
EditNameListener listener;
public DialogFragment setOnEditNameListener(EditNameListener listener){
this.listener=listener;
return this;
}