如何使用对话框片段?(不推荐使用showDialog)Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20405070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Dialog Fragment? (showDialog deprecated) Android
提问by Lion789
I understand that there is this documentation
我知道有这个文档
http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog
http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog
but as a new Android/Java learner it is not easy to understand the amount of code involved from writing a simple alert dialog that pops up with 2 options (yes/no) message.
但作为一个新的 Android/Java 学习者,编写一个弹出 2 个选项(是/否)消息的简单警报对话框并不容易理解所涉及的代码量。
Here is the code I currently have in my MainActivity file:
这是我目前在 MainActivity 文件中的代码:
final private int RESET_DIALOG = 0;
private OnClickListener resetButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(RESET_DIALOG);
}
};
protected android.app.Dialog onCreateDialog(int id) {
switch(id) {
case RESET_DIALOG:
AlertDialog.Builder builder = new Builder(this);
return builder
.setMessage("Are you sure you want to reset the count?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did not reset!", 5).show();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did Reset!", 5).show();
}
})
.create();
}
return null;
};
This is my attempt to following the instructions on the android site: Main Activity file:
这是我尝试按照 android 站点上的说明进行操作:主活动文件:
final private int RESET_DIALOG = 0;
private OnClickListener resetButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainDialog.class);
startActivity(intent);
}
};
protected android.app.Dialog onCreateDialog(int id) {
switch(id) {
case RESET_DIALOG:
AlertDialog.Builder builder = new Builder(this);
return builder
.setMessage("Are you sure you want to reset the count?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did not reset!", 5).show();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did Reset!", 5).show();
}
})
.create();
}
return null;
};
Then created a MainDialog class: (I am actually lost in how to do this correctly or apply it)
然后创建了一个 MainDialog 类:(我实际上迷失了如何正确执行或应用它)
package com.proteintracker;
import android.support.v4.app.DialogFragment;
public class MainDialog extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
}
I am not sure if I was suppose to create a new class for the fragment and how to apply it to my current dialog in the activity screen.
我不确定是否要为片段创建一个新类,以及如何将它应用到活动屏幕中的当前对话框。
回答by HotIceCream
You can show your DialogFragment
like this:
你可以这样展示你的DialogFragment
:
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string.alert_dialog_two_buttons_title);
newFragment.show(getFragmentManager(), "dialog");
}
In you fragment dialog you should override onCreateDialog
and return you instance of simple Dialog
, for example AlertDialog
.
例如,在片段对话框中,您应该覆盖onCreateDialog
并返回 simpleDialog
实例AlertDialog
。
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
AlertDialog.Builder builder = new Builder(this);
return builder
.setMessage("Are you sure you want to reset the count?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did not reset!", 5).show();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did Reset!", 5).show();
}
})
.create();
}
}
回答by Jawad Zeb
Alert with custom view
带有自定义视图的警报
public class MyAlertDialogFragment extends DialogFragment {
public static final String TITLE = "dataKey";
public static MyAlertDialogFragment newInstance(String dataToShow) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putString(TITLE, dataToShow);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String mDataRecieved = getArguments().getString(TITLE,"defaultTitle");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.alert_layout, null);
TextView mTextView = (TextView) view.findViewById(R.id.textview);
mTextView.setText(mDataRecieved);
setCancelable(false);
builder.setView(view);
Dialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
return dialog;
}
}
And Alert with YesNoDialog interface
和警报与 YesNoDialog 界面
public class MyAlertDialogFragment extends DialogFragment {
public static final String TITLE = "dataKey";
private OnYesNoClick yesNoClick;
public static MyAlertDialogFragment newInstance(String dataToShow ) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putString(TITLE, dataToShow);
frag.setArguments(args);
return frag;
}
public void setOnYesNoClick(OnYesNoClick yesNoClick) {
this.yesNoClick = yesNoClick;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String mDataRecieved = getArguments().getString(TITLE,"defaultTitle");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder
.setMessage("Message to Show")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if(yesNoClick != null)
yesNoClick.onNoClicked();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if(yesNoClick != null)
yesNoClick.onYesClicked();
}
});
Dialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
return dialog;
}
public interface OnYesNoClick{
void onYesClicked();
void onNoClicked();
}
}
Use it like
使用它就像
private void showYesNoDialog(){
MyAlertDialogFragment yesNoAlert = MyAlertDialogFragment.newInstance(
"Data to Send");
yesNoAlert.show(getFragmentManager(), "yesNoAlert");
yesNoAlert.setOnYesNoClick(new MyAlertDialogFragment.OnYesNoClick() {
@Override
public void onYesClicked() {
//yes or ok clicked
}
@Override
public void onNoClicked() {
//no or cancel clicked
}
});
}
回答by iflorit
Example of DialogFragment using Sherlock
使用 Sherlock 的 DialogFragment 示例
FragmentManager fm = getSherlockActivity().getSupportFragmentManager();
DialogFragment dialog = new DialogFragment(){
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder
.setTitle(getString(R.string.delete)+"?")
.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do something
}
})
.setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
};
dialog.setCancelable(true);
dialog.show(fm, "DELETE_DIALOG_FRAGMENT");
回答by Sapan Diwakar
You can show the dialog like this:
您可以像这样显示对话框:
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to reset the count?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did not reset!", 5).show();
}
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "Did Reset!", 5).show();
}
})
.create().show();