0

したがって、私の現在の問題は、ボタンが押されたときにダイアログ ボックスを更新するエレガントな方法が見つからないことです。機能的には、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() で行ったように、これを処理しようとしましRunnablerunOnUiThread(updateScreen)。ベースアクティビティに最適です。

私はいくつかのコピーパスタを作成し、updateSellScreen() を作成しようとしましたが、それをカスタム ダイアログのテキストビューにフックするようにしましたが、アラート クラスを解決できません。

これは、すべてを破棄してカスタムビューを作成するだけで可能ですか(これは、Androidプログラミングに新たに取り組もうとするのは非常に嫌です...)

4

2 に答える 2

1

ダイアログを作成するアクティビティでは、ダイアログ、テキストビューなどのプライベート変数を宣言できます。そうすれば、アクティビティのどこからでもアクセスできるようになります。

        dialogA = new Dialog(myActivity.this, android.R.style.Theme_Dialog);
    dialogA.setContentView(R.layout.myDialog);
    // ...      
    tv1 = (TextView) dialogA.findViewById(R.id.textView1);

    Button b1 = (Button) dialogA.findViewById(R.id.button1);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String s1 = tv1.getText().toString();
            Toast.makeText(myActivity.this, s1, Toast.LENGTH_SHORT).show();
            dialogA.cancel();
        }
    });

    dialogA.show();
于 2011-04-19T00:56:16.697 に答える
1

TextViews を final として宣言します。テキストを設定することはできますが、変数参照を再割り当てすることはできません。TextView はダイアログのインスタンス変数ではなく、ダイアログを作成するメソッドのインスタンスであるため、alert.tv を実行しないでください。これは簡単な方法です。TextView をアクティビティのインスタンス変数として宣言し、ハンドラーを介してそれらを更新することもできます。

alert.setTitle("Sell how many "+(masterRes.get(currentResIndex).getName())+"?");
alert.setContentView(R.layout.selldialog);
final TextView tvsellAll = (TextView) alert.findViewById(R.id.sellAllText);
final 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();
               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)));
               tvsellAmt.setText("Sell Amount (0-"+String.valueOf(masterRes.get(currentResIndex).getHeld())+")");


           }
        }
    });
于 2011-04-19T02:22:41.090 に答える