【发布时间】:2013-12-22 16:37:45
【问题描述】:
我知道有这个文档
http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog
但作为一个新的 Android/Java 学习者,要理解编写一个弹出两个选项(是/否)消息的简单警报对话框所涉及的代码量并不容易。
这是我目前在 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;
};
这是我尝试按照 android 网站上的说明进行操作:Main Activity 文件:
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;
};
然后创建了一个 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;
}
}
我不确定我是否应该为片段创建一个新类以及如何将它应用到活动屏幕中的当前对话框。
【问题讨论】:
标签: android android-fragments android-alertdialog