动态更改AlertDialog中按钮的文本

时间:2012-11-10 11:58:02

标签: android alertdialog settext

编辑:以下代码有效!错误发生在我的代码中的其他地方......

原始帖子如下......

编辑结束。

我有一个textView,它使用OnClickListener()加载onResume(),打开一个AlertDialog。 AlertDialog有一些文本和三个按钮,setPositivtButton,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代码中执行此操作?

编辑:

稍微更改了代码(变量=!变量),没有任何变化。 也用Dialog尝试了这个但没有成功......

我是android的新手,但是在onResume()中设置TextView和Dialog是不是可以改变任何东西,直到使用onClickListener再次加载onResume(),就像我一样?

- 托米

1 个答案:

答案 0 :(得分:-1)

创建一个单例类来保存你想要的值。然后使用该单例类的实例

访问它