- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试找到重用显示自定义标题的对话框的方法,然后将“是/否”单击发送到启动该对话框的函数。
我有两个按钮,Save 和 Dismiss,它们都调用 Yes/No 对话框,一个显示“Do you want to save”,另一个显示“Dismiss changes?”。
我认为我的程序很“脏”,但我想它可以工作,但我的问题是“View view”变量,我不知道如何将它从 Activity 传递到 Dialog,所以我可以使用它调用启动对话框的函数。
提前致谢,赫尔尼德兹
我的 Activity 的 .java(它的 fragment )
public void open_HH_Fragment_YesNo(View view, String aux_title, String aux_function)
{
Bundle bundle=new Bundle();
bundle.putString("setMessage", aux_title);
bundle.putString("callingFunction", aux_function);
DialogFragment newFragment = new HH_Fragment_YesNo();
newFragment.setArguments(bundle);
newFragment.show(getSupportFragmentManager(), "HH_Fragment_YesNo");
}
public void SaveChanges(View view, String aux_YesNo)
{
if (aux_YesNo == "")
{
Toast.makeText(this, "Save changes?", Toast.LENGTH_SHORT).show();
open_HH_Fragment_YesNo(view, "Save changes?", "SaveChanges");
}
else if (aux_YesNo == "Yes")
{
Toast.makeText(this, "Saving changes", Toast.LENGTH_SHORT).show();
}
else if (aux_YesNo == "No")
{
Toast.makeText(this, "Save Cancelled", Toast.LENGTH_SHORT).show();
}
}
public void DismissChanges(View view, String aux_YesNo)
{
if (aux_YesNo == "")
{
Toast.makeText(this, "Dismiss changes?", Toast.LENGTH_SHORT).show();
open_HH_Fragment_YesNo(view, "Dismiss changes?", "DismissChanges");
}
else if (aux_YesNo == "Yes")
{
Toast.makeText(this, "Dismiss OK", Toast.LENGTH_SHORT).show();
Close(view);
}
else if (aux_YesNo == "No")
{
Toast.makeText(this, "Dismiss Cancelled", Toast.LENGTH_SHORT).show();
}
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the HH_Fragment_YesNo.YesNoDialogListener interface
@Override
public void onDialogPositiveClick(DialogFragment dialog, View view, String aux_function)
{
// User touched the dialog's positive button
Toast.makeText(this, "User clicked on Yes", Toast.LENGTH_SHORT).show();
if (aux_function == "SaveChanges")
{
SaveChanges(view, "Yes");
}
else if (aux_function == "DismissChanges")
{
DismissChanges(view, "Yes");
}
}
@Override
public void onDialogNegativeClick(DialogFragment dialog, View view, String aux_function)
{
Toast.makeText(this, "User clicked on NO", Toast.LENGTH_SHORT).show();
if (aux_function == "SaveChanges")
{
SaveChanges(view, "No");
}
else if (aux_function == "DismissChanges")
{
DismissChanges(view, "No");
}
}
我的Dialog的.java(完整)
public class HH_Fragment_YesNo extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
String setMessage = getArguments().getString("setMessage");
final String callingFunction = getArguments().getString("callingFuntion");
builder
.setMessage(setMessage) // R.string.dialog_fire_missiles
.setPositiveButton("Sí", new DialogInterface.OnClickListener() // R.string.fire
{
public void onClick(DialogInterface dialog, int id)
{
// Exit without saving
mListener.onDialogPositiveClick(HH_Fragment_YesNo.this, view, callingFunction);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() // R.string.cancel
{
public void onClick(DialogInterface dialog, int id)
{
// User cancelled the dialog
mListener.onDialogNegativeClick(HH_Fragment_YesNo.this, view, callingFunction);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface YesNoDialogListener
{
public void onDialogPositiveClick(DialogFragment dialog, View view, String aux_Function);
public void onDialogNegativeClick(DialogFragment dialog, View view, String aux_Function);
}
// Use this instance of the interface to deliver action events
YesNoDialogListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try
{
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (YesNoDialogListener) activity;
}
catch (ClassCastException e)
{
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
}
最佳答案
完整的解决方案试试这个
1) 创建接口(interface)
import android.content.DialogInterface;
public interface AlertMagnatic {
public abstract void PositiveMethod(DialogInterface dialog, int id);
public abstract void NegativeMethod(DialogInterface dialog, int id);
}
2) 确认对话框的泛化方法。
public static void getConfirmDialog(Context mContext,String title, String msg, String positiveBtnCaption, String negativeBtnCaption, boolean isCancelable, final AlertMagnatic target) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
int imageResource = android.R.drawable.ic_dialog_alert;
Drawable image = mContext.getResources().getDrawable(imageResource);
builder.setTitle(title).setMessage(msg).setIcon(image).setCancelable(false).setPositiveButton(positiveBtnCaption, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
target.PositiveMethod(dialog, id);
}
}).setNegativeButton(negativeBtnCaption, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
target.NegativeMethod(dialog, id);
}
});
AlertDialog alert = builder.create();
alert.setCancelable(isCancelable);
alert.show();
if (isCancelable) {
alert.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
target.NegativeMethod(null, 0);
}
});
}
}
3)使用方法
getConfirmDialog(getString(R.string.logout), getString(R.string.logout_message), getString(R.string.yes), getString(R.string.no), false,
new AlertMagnatic() {
@Override
public void PositiveMethod(final DialogInterface dialog, final int id) {}
@Override
public void NegativeMethod(DialogInterface dialog, int id) {
}
});
关于android - 如何在 Android 上重复使用 AlertDialog 表示是/否?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18413925/
编辑 感谢您的所有评论和回答。我很困惑,在尝试不同的答案后我能够解决问题,但现在无法重现问题???我不知道发生了什么事。 我想做的是通过 id 获取一个元素,但是这个 id 中包含多个点,因为 id
我正在尝试对 bool 字段进行是/否选择。默认小部件是复选框输入。但是,如果我使用 Select 覆盖默认小部件,则会得到:NameError: Select is not defined 我认为这
我有一个背景图像,所有 View 中的图像都相同,但导航栏 traslucent 默认设置为 YES 并且背景不均匀。我尝试了各种解决方案,但没有任何改变:( 我的 AppDelegate.h #im
所以我有一个要分析的数据框。问题是数据框中有一堆 1 和 0(1 是,0 是否),而不是是/否。如何修改数据框以使其不是 1 和 0,而是 Yes 和 No,以便我可以使用逻辑回归?我确信有一个我没有
我是 XML/XSL 的新手(比如 2 天新)。我有一行我正在做一个 xsl:value-of 选择,它返回一个 True/False 属性。我想让它显示是/否,但我试图这样做没有成功。下面是我目前的
我正在尝试写一个简单的 do..until循环,它不起作用: $yesNo = Read-Host -Prompt 'Do you want to add alternative DNS names
这个问题已经有答案了: How to use this boolean in an if statement? (8 个回答) 已关闭 7 年前。 import java.util.Scanner;
提示问题生成器 class SynthesisPromptGenerator: def wait_key(self): ''' Wait for a key press on
我想自动给出输入是/否,而不是通过键盘/用户输入。 user_choice = raw_input("Are you sure want to remove all item (y/n):\n") i
我需要在文件存在时向用户显示一些消息,显示消息“文件存在...你想覆盖它吗??” if (File.Exists(binaryFilePath)) { Program.DisplayMessa
我在一个表格中有 5 个问题,其中一些的回答是"is"/“否”。一旦用户选择其中一个按钮,我希望能够切换是和否按钮。我遇到的问题是,如果其中一个问题得到了回答,并且我回答了下一个问题,它会从所有其他问
如何使用 bash 脚本在文本文件的某行/之后搜索字符串? 例如我想搜索第一次出现的 "version:" 字符串,但不是在文件开头而是在第 1 行。说 35 其中包含文本 *-disk:0 这样我就
目前我正在创建一个 Alexa 技能来处理 FAQ 问题和导致直接回复的问题,但我对 alexa 何时需要另一个回复有疑问。 例如,如果用户询问, “我应该吃什么” Alexa 会回复“你想吃意大利面
如果用户反复按下后退按钮,我需要一种方法来检测他们何时处于我的任务/应用程序的最后一个 Activity 并显示“你想退出吗?”在他们返回主屏幕或他们之前运行的任何应用程序之前的对话框。 Hook o
我有一个充满数据的表格,在行的末尾我有一个删除选项。 与其链接到一个全新的页面来确认删除,不如将“删除”链接拆分为一个是/否链接,根据单击的内容,删除行或变回“删除”,这样会很整洁。我知道 reddi
使用一些批处理文件,我想在 Java Keystore 中添加不受信任的自签名证书。 命令是 %JAVA_HOME%/bin/keytool -import -v -trustcacerts -ali
我有一个充满 IP 地址的电子表格(“IP LIST”)。在该工作簿中,我有第二个电子表格代表 IP 地址部分,该部分属于 IP 地址的较大部分中的打印机(“打印机”)。我已经在“IP LIST”上确
一旦用户单击"is"并在输入框中输入一个值,但改变主意并单击“否”选项,但输入值仍然存在,如何删除该值 Do you offer rented call center seating on
因此,我尝试验证用户是否输入了"is"或“否”,并继续询问,直到他们输入其中之一。这是我到目前为止的代码。 System.out.println("Would you like a Diamond i
我目前正在关注 Erik Roberts 所著的《Java 的艺术与科学》一书。当在代码中测试是/否/替代答案时,我会因为没有答案而被问两次。 /* * File: YesNoQuestion.ja
我是一名优秀的程序员,十分优秀!