所以,我当前的问题是,当按下按钮时,我找不到一种优雅的方式来更新对话框。我可以通过dismiss() 和show() 在功能上实现相同的结果,但这很难看。
假设这个对话框有 3 个按钮,用于销售玩家拥有的小部件。Sell All、Sell 10 和 Sell X(使用 EditText 输入的金额)。如果玩家推动 Sell 10,我希望对话框持续存在,但也希望使用新的小部件数量更新它的文本视图。
自定义对话框的 XML 布局的相关部分:
<LinearLayout android:id="@+id/linearLayout3" android:layout_height="wrap_content" android:layout_width="match_parent">
<TextView android:id="@+id/sell10Text" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content" android:layout_weight="2"></TextView>
<Button android:text="Sell 10" android:enabled="false" android:layout_width="wrap_content" android:id="@+id/sell10Button" android:layout_height="wrap_content" android:layout_weight="1"></Button>
</LinearLayout>
对话创建的相关部分:
final Dialog alert = new Dialog(this);
alert.setTitle("Sell how many "+(masterRes.get(currentResIndex).getName())+"?");
alert.setContentView(R.layout.selldialog);
TextView tvsellAll = (TextView) alert.findViewById(R.id.sellAllText);
TextView tvsell10 = (TextView) alert.findViewById(R.id.sell10Text);
//etc etc more handles, including buttons
tvsellAll.setText("Sell All ("+String.valueOf(masterRes.get(currentResIndex).getHeld())+") - $"+String.valueOf(calcCost(masterRes.get(currentResIndex).getHeld())));
tvsell10.setText("Sell 10 - $"+String.valueOf(calcCost(10)));
// etc etc more setTexts
btnsell10.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
if (v.isEnabled()) {
int y=masterRes.get(currentResIndex).getHeld();
masterRes.get(currentResIndex).setHeld(y-10);
held -= 10;
money += (calcCost(10));
updateScreen();
alert.tvsellAll.setText("Sell All ("+String.valueOf(masterRes.get(currentResIndex).getHeld())+") - $"+String.valueOf(calcCost(masterRes.get(currentResIndex).getHeld())));
alert.tvsell10.setText("Sell 10 - $"+String.valueOf(calcCost(10)));
alert.tvsellAmt.setText("Sell Amount (0-"+String.valueOf(masterRes.get(currentResIndex).getHeld())+")");
}
}
});
// etc etc other button handlers, alert.show() at the end
现在显然按钮内的 setTexts 无法解析,因为他们看不到我创建的警报,他们只看到 OnClickListener。
我尝试像使用主要活动的更新程序 updateScreen() 一样处理此问题,它是一个Runnable
,它是一个很长的 setTexts 和/或无效列表,并且是runOnUiThread(updateScreen)
. 非常适合基础活动。
我做了一些copypasta并尝试制作一个updateSellScreen(),让它挂接到自定义对话框的文本视图中,但它无法解析警报类......我现在有点迷路了。
在不破坏所有内容并仅创建自定义视图的情况下,这是否可能(我非常不愿意尝试将这一新鲜事物处理到 Android 编程中......)