DialogFragment:使用AlertDialog与自定义布局 [英] DialogFragment : Using AlertDialog with custom layout
本文介绍了DialogFragment:使用AlertDialog与自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我重写我与应用程序片段API
支持。在原来的应用程序,我有一个 AlertDialog
是这样创建的:
I'm rewriting my application with Fragments API
support. In the original application I have an AlertDialog
that is created like this :
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.button_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
ListView mListView = (ListView) view.findViewById(R.id.mblist);
builder.setTitle(getString(R.string.sysinfo)).setNeutralButton(
getString(R.string.okay), null);
Cursor dcursor = propManager.propCursor();
if (dcursor.moveToFirst()) {
startManagingCursor(dcursor);
String[] from = { Constants.NAME_DET,
Constants.VALUE_DET };
int[] to = { R.id.mbname, R.id.mbvalue };
SimpleCursorAdapter dadapter = new SimpleCursorAdapter(this,
R.layout.info_list_row, dcursor, from, to);
mListView.setAdapter(dadapter);
}
final Dialog dialog = builder.create();
dialog.show();
我如何可以显示通过DialogFragment相同的对话框?我已阅读文档,但都无法搞清楚如何处理这个问题。
How can I display the same dialog via a DialogFragment? I have read the documentation but have trouble figuring out how to proceed with this.
推荐答案
我很惊讶,没有人回答。这是解决方案:
I'm surprised that nobody answered. This is the solution:
public class PropDialogFragment extends DialogFragment {
private PropDialogFragment() { /*empty*/ }
/** creates a new instance of PropDialogFragment */
public static PropDialogFragment newInstance() {
return new PropDialogFragment();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//getting proper access to LayoutInflater is the trick. getLayoutInflater is a //Function
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.my_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setTitle(getActivity().getString(R.string.sysinfo)).setNeutralButton(
getActivity().getString(R.string.okay), null);
return builder.create();
}
}
您可以使用显示对话框:
You can show the dialog using :
private void showDialog() {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = PropDialogFragment.newInstance();
newFragment.show(ft, "dialog");
}
这篇关于DialogFragment:使用AlertDialog与自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文