2011-02-02 95 views
0

在我的主要布局XML我:如何在线程中设置Android中按钮上的文本?

<LinearLayout android:id="@+id/LinearLayout01" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 

<LinearLayout android:orientation="vertical" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/LinearLayout02"> 

<Chronometer android:text="@+id/Chronometer01" android:id="@+id/Chronometer01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Chronometer> 


<Button android:text="@string/hints" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button01"></Button> 

</LinearLayout> 
<com.touchpanel.Panel android:id="@+id/SurfaceView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
</com.touchpanel.Panel> 

</LinearLayout> 

在我的面板类,我产生一个线程来进行绘制本身。我的问题是,我可以访问其他线性布局中的按钮来更改文本吗? (这会发生很多,文字是动态的)

我尝试在使用此布局的活动中声明按钮,但我无法从Panel类访问它。我可以编写一个将文本作为参数放入按钮的函数,但如果从Panel类调用该函数,它将强制关闭。如果我注释掉“button1.setText(”Text“);”行,它处理函数调用罚款。

试图宣布在面板类的按钮,如:

private Button b1; //Declared at the beginning of the class 

//in constructor of Panel class: 
b1 = (Button)findViewById(R.id.Button01); 
b1.setText("Hello"); 

它声明的按钮,并设置它的罚款。但是当我尝试使用“b1.setText”这一行时,它强制关闭。

有什么建议吗?

+0

您可能想在这里问这个问题:http://android.stackexchange.com/它是一个StackExchange社区,像StackOverflow,致力于Android的问题。 – Josh 2011-02-02 21:28:45

+5

@Josh:Android SE上的开发问题[显式偏离主题](http://android.stackexchange.com/faq)。他们应该在这里张贴,就像OP所做的一样。 Android SE适用于Android设备的**用户**,而不是开发人员。 – eldarerathis 2011-02-02 21:30:37

回答

4

您无法从非UI线程修改UI。使用Handler。或者,拨打Button致电post()。或者,使用runOnUiThread()。或者,转储线程并使用AsyncTask

相关问题