eclipse 通过按钮更新自定义对话框内的文本视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5710348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:11:53  来源:igfitidea点击:

Updating a textview inside a custom dialog via a button

androideclipsedialogsettext

提问by Eric

So, my current issue is that I can't find an elegant way to update a dialog box when a button is pressed. I can achieve functionally the same result by dismiss() and show(), but that is ugly.

所以,我目前的问题是我找不到一种优雅的方法来在按下按钮时更新对话框。我可以通过dismiss() 和show() 在功能上实现相同的结果,但这很丑陋。

Lets say this dialog has 3 buttons, for selling widgets that the player has. Sell All, Sell 10, and Sell X (amount entered with a EditText). I'd like for the dialog to persist if the player pushes Sell 10, but also to update it's textviews with the new count of widgets.

假设这个对话框有 3 个按钮,用于销售玩家拥有的小部件。Sell All、Sell 10 和 Sell X(使用 EditText 输入的金额)。如果玩家按下 Sell 10,我希望对话框保持不变,而且还使用新的小部件数量更新它的文本视图。

Pertinent part of the XML layout of the custom dialog:

自定义对话框的 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>

Pertinent part of the dialog creation:

对话框创建的相关部分:

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

Now obviously the setTexts within the button can't resolve, as they can't see the alert I created, they just see OnClickListener.

现在显然按钮内的 setTexts 无法解析,因为他们看不到我创建的警报,他们只能看到 OnClickListener。

I tried handling this like I did with my main activity's updater updateScreen(), which is a Runnable, that is a long list of setTexts and/or invalidates, and is runOnUiThread(updateScreen). Works great for the base activity.

我尝试像处理我的主要活动的更新程序 updateScreen() 那样处理这个问题,它是一个Runnable,这是一长串 setTexts 和/或无效,并且是runOnUiThread(updateScreen). 非常适合基础活动。

I did some copypasta and tried to make a updateSellScreen(), get it to hook into the custom dialog's textviews, but it can't resolve the alert class... I'm kind of lost now.

我做了一些 copypasta 并尝试制作一个 updateSellScreen(),让它挂钩到自定义对话框的文本视图中,但它无法解析警报类......我现在有点迷失了。

Is this even possible without trashing everything and just creating a custom view (which I am very averse to trying to tackle this fresh into Android programming...)

这甚至可以在不破坏所有内容并仅创建自定义视图的情况下实现(我非常反对尝试将这种新鲜事物引入 Android 编程......)

回答by bob

In activity where you creates your dialog, you can declare private variables of dialog, textviews, etc, then they will be accessible anywhere in activity.

在您创建对话框的活动中,您可以声明对话框、文本视图等的私有变量,然后它们就可以在活动的任何地方访问。

        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();

回答by Joe

Declare your TextViews as final. You'll still be able to set their texts, it just means you won't be able to reassign the variable references. Don't do alert.tv as the TextView is not an instance variable of your dialog, but rather of the method with which you are creating your dialog. This is the easy way. You could also declare your TextViews as instance variables of your Activity and then update them through a handler.

将您的 TextViews 声明为 final。您仍然可以设置它们的文本,这只是意味着您将无法重新分配变量引用。不要执行 alert.tv,因为 TextView 不是对话框的实例变量,而是用于创建对话框的方法。这是最简单的方法。您还可以将 TextViews 声明为 Activity 的实例变量,然后通过处理程序更新它们。

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())+")");


           }
        }
    });