2011-04-19 68 views
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()所做的那样,它是一个Runnable,它是一个很长的setTexts和/或invalidate列表,并且是runOnUiThread(updateScreen)。适用于基本活动。

我做了一些副本,并试图做一个updateSellScreen(),让它挂钩到自定义对话框的textviews,但它不能解决警报类...我现在有点失落。

这甚至可能没有捣毁一切,只创建一个自定义视图(这我很不愿意试图解决这个食入的Android编程...)

+0

为了澄清,列出的'updateScreen();'列表只处理主页的活动。我无法调和足够的事情来让它在对话中起作用,因此也就成了问题。 – Eric 2011-04-19 00:39:32

回答

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


      } 
     } 
    }); 
+0

这将工作,并感谢你对'final'的好的解释,并帮助我更好地处理变量作用域在Java – Eric 2011-04-19 04:27:35

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

我的代码中已经有了这个结构。我想要做的是从'onClick'方法中,做一个tv1.setText(“foo”); – Eric 2011-04-19 00:59:35

+0

在您的代码中,tvsellAll是对话框的本地对象,在我的代码中,tv1属于父级活动。 – bob 2011-04-19 01:05:47

+0

当我使用类似的代码时,我仍然得到一个编译器错误,在onClick内部,我使用tv1.setText(“foo”)::不能引用在不同方法中定义的内部类中的非最终变量tvsell10。如果我让它最终决定,它不会让我设置。我很困惑:( – Eric 2011-04-19 01:49:14

相关问题