通过一个按钮来更新自定义对话框里面一个TextView [英] Updating a textview inside a custom dialog via a button

查看:92
本文介绍了通过一个按钮来更新自定义对话框里面一个TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我现在的问题是,我无法找到一个优雅的方式,当一个按钮pssed $ P $更新对话框。我能实现功能相同的结果驳回()和show(),但是这是丑陋的。

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.

可以说,这个对话框有3个按键,出售小部件的玩家。卖掉所有,卖10,和销售X(与输入的EditText量)。我想为对话持续下去,如果播放器推卖10,而且还以更新它的textviews与小窗口的新的计数

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.

自定义对话框中的XML布局的相关部分:

Pertinent part of the XML layout of the custom dialog:

<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

现在显然在按钮中的setTexts解决不了,因为他们看不到我创建的警觉,他们只看到OnClickListener。

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

我试着处理这就像我与我的主要活动的更新updateScreen(),这是一个的Runnable ,即setTexts和/或无效的一个长长的清单,并是 runOnUiThread(updateScreen)。为基本活动的伟大工程。

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.

我做了一些copypasta,并试图使updateSellScreen(),把它勾到自定义对话框的textviews,但它无法解决该警报类......那种我现在失去了。

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.

这甚至可能没有捣毁一切,只创建一个自定义视图(这我很反感试图解决这个食入的Andr​​oid编程...)

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...)

推荐答案

声明的TextViews的决赛。您仍然可以设置自己的文本,它只是意味着你将无法重新分配的变量引用。不要做alert.tv作为TextView的是不是你的对话框的一个实例变量,而是与你所创建对话框的方法。这是最简单的方式。您也可以宣布你TextViews作为活动的实例变量,然后通过处理程序进行更新。

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.

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


           }
        }
    });

这篇关于通过一个按钮来更新自定义对话框里面一个TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆