编辑:下面的代码有效!错误在我的代码中的其他地方......
原帖如下...
编辑结束。
我有一个在 onResume() 中加载的 textView,它带有一个打开 AlertDialog 的 OnClickListener()。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();
};
});
}
这段代码是否可以通过一些 tweeks 来实现,还是我必须重写整个代码?每次单击 textView 时是否应该创建 AlertDialog 或者是否可以直接在 setNeutralButton 代码中执行此操作?
编辑:
稍微更改了代码(变量=!变量),没有任何变化。还尝试了一个没有成功的对话框......
我是 android 新手,但是在 onResume() 中设置 TextView 和 Dialog 是否会导致在使用 onClickListener 再次加载 onResume() 之前无法更改任何内容?
——汤米