したがって、私の現在の問題は、ボタンが押されたときにダイアログ ボックスを更新するエレガントな方法が見つからないことです。機能的には、dismiss() と show() によって同じ結果を得ることができますが、それは醜いです。
このダイアログには、プレーヤーが持っているウィジェットを販売するための 3 つのボタンがあるとします。すべて売却、10 売却、および 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
たrunOnUiThread(updateScreen)
。ベースアクティビティに最適です。
私はいくつかのコピーパスタを作成し、updateSellScreen() を作成しようとしましたが、それをカスタム ダイアログのテキストビューにフックするようにしましたが、アラート クラスを解決できません。
これは、すべてを破棄してカスタムビューを作成するだけで可能ですか(これは、Androidプログラミングに新たに取り組もうとするのは非常に嫌です...)