Android 动态更改AlertDialog中按钮的文本

Android 动态更改AlertDialog中按钮的文本,android,android-alertdialog,settext,Android,Android Alertdialog,Settext,编辑:下面的代码工作!错误在我代码的其他地方 原帖如下 编辑结束 我有一个文本视图,在onResume中加载了一个OnClickListener,它打开了一个AlertDialog。AlertDialog有一些文本和三个按钮,即SetPositiveButton、setNegativeButton和setNeutralButton。每次打开AlertDialog时,我都想根据一些变量更改中性按钮的文本。我试过了,但没有成功 enter code here protected void OnRes

编辑:下面的代码工作!错误在我代码的其他地方

原帖如下

编辑结束

我有一个文本视图,在onResume中加载了一个OnClickListener,它打开了一个AlertDialog。AlertDialog有一些文本和三个按钮,即SetPositiveButton、setNegativeButton和setNeutralButton。每次打开AlertDialog时,我都想根据一些变量更改中性按钮的文本。我试过了,但没有成功

enter code here
protected void OnResume() {
    TextView text = new TextView (this);
    text.setTextColor(res.getColor(R.color.white));
    text.setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {                 
            final String str = (String) v.getTag();
            String buttonText = "Jump";
            AlertDialog.Builder showinf = new AlertDialog.Builder(v.getContext());
            showinf.setTitle("SomeTitle");
            showinf.setMessage("someMeassage");
            showinf.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            if (variable == true) buttonText = "Sit";
                else buttonText = "Jump";
            variable = !variable; //For testing purposes...
            showinf.setNeutralButton(buttonText, new DialogInterface.OnClickListener()     {                        
                public void onClick(DialogInterface dialog, int id) {
                    // Do something
                }
            });
            showinf.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // Do something
                }
            });
            showinf.show();
        };
    });
}
这段代码有没有可能用一些技巧,或者我必须重写整个代码?是否应在每次单击textView时创建AlertDialog,或者是否可以在setNeutralButton代码中直接创建AlertDialog

编辑:

将代码更改为位变量=!变量,没有任何变化。 还尝试了一个对话,但没有成功

我是android新手,但在onResume中设置TextView和Dialog是否会导致在onResume再次使用onClickListener加载之前无法更改任何内容

-
Tommy

创建一个singleton类来保存所需的值。然后使用该单例类的实例访问它

是否确定变量布尔值正在更改?是。甚至像这样设置变量:variable=!变量