【问题标题】:Updating a textview inside a custom dialog via a button通过按钮更新自定义对话框中的文本视图
【发布时间】:2011-04-19 00:37:31
【问题描述】:

所以,我当前的问题是我找不到在按下按钮时更新对话框的优雅方法。我可以通过 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,它是一个很长的 setText 和/或无效列表,并且是 runOnUiThread(updateScreen)。非常适合基础活动。

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

这是否可能在不破坏所有内容并仅创建自定义视图的情况下实现(我非常反对尝试在 Android 编程中重新解决这个问题...)

【问题讨论】:

  • 澄清一下,列出的行:updateScreen(); 仅处理主页的活动。我无法协调事物的范围足以让它在对话框中工作,因此问题。

标签: android eclipse dialog settext


【解决方案1】:

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


           }
        }
    });

【讨论】:

  • 这会起作用,感谢您对 final 的良好解释,并帮助我更好地处理 java 中的变量范围
【解决方案2】:

在你创建对话框的activity中,你可以声明对话框、textviews等的私有变量,然后它们就可以在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();

【讨论】:

  • 我的代码中已经有了这个结构。我想做的是从onClick 方法中做一个 tv1.setText("foo");
  • 在您的代码中,tvsellAll 是对话框的本地,在我的代码中,tv1 属于父活动。
  • 当我使用类似的代码时,当我在 onClick 内部使用 tv1.setText("foo") :: 无法在内部类中引用非最终变量 tvsell10 时,仍然会出现编译器错误用不同的方法定义。如果我让它最终,它不会让我设置。我很困惑:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 2021-02-06
  • 1970-01-01
  • 2011-07-19
相关资源
最近更新 更多