gpt4 book ai didi

android - 单击 DialogFragment 正按钮后如何执行操作

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:17 25 4
gpt4 key购买 nike

我根据 Android 文档创建了以下 DialogFragment:

公共(public)类 PayBillDialogFragment 扩展 DialogFragment{

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState){

final Bundle b = this.getArguments();
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Paga bollettino")
.setPositiveButton("Paga", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!


}
})
.setNegativeButton("Cancella", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();

}





}

从另一个 fragment (一个 ListFragment)中,当单击列表的一行时,应该打开 DialogFragment 并且在按下 DialogFragment 的肯定按钮后我希望能够删除 ListFragment 的选定行并调用执行与删除相关的远程操作的方法。我按如下方式实现了 ListFragment:

public static class ListFragment extends android.support.v4.app.ListFragment {



ArrayList<String> listItems=new ArrayList<String>();


ArrayAdapter<String> adapter;


public static final String ARG_SECTION_NUMBER = "section_number";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.list_fragment_view,
container, false);


ListView lv = (ListView)rootView.findViewById(android.R.id.list);

}});
adapter=new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
return rootView;
}



@Override
public void onListItemClick(ListView l, View v, int position, long id) {



//opening the dialogfragment


}


}




}

我不知道的是单击DialogFragment 的肯定按钮后的 Action 如何处理。你能帮助我吗?

编辑:澄清一下,这是工作流程:单击列表 -> 显示 DialogFragment -> 单击 DialogFragment 后从列表中删除元素。

最佳答案

这就是我处理 fragment 和对话框 fragment 之间通信的方式

示例 fragment :

public class MainFragment extends Fragment {

private static final int REQ_CODE = 1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.main_fragment, container, false);
Button b = (Button) v.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(MainFragment.this, REQ_CODE);
dialog.show(getFragmentManager(), "dialog");
}
});
return v;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getActivity(), "Result: " + resultCode,
Toast.LENGTH_SHORT).show();
super.onActivityResult(requestCode, resultCode, data);
}

}

DialogFragment 示例:

public class MyDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("My dialog message")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
notifyToTarget(Activity.RESULT_OK);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
notifyToTarget(Activity.RESULT_CANCELED);
}
});
return builder.create();
}

private void notifyToTarget(int code) {
Fragment targetFragment = getTargetFragment();
if (targetFragment != null) {
targetFragment.onActivityResult(getTargetRequestCode(), code, null);
}
}

}

这是我在改变方向时也能使用的唯一方法。

关于android - 单击 DialogFragment 正按钮后如何执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15970196/

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