TextView的方法的setText()不工作方法的onClick() [英] TextView method setText() not working in method onClick()
本文介绍了TextView的方法的setText()不工作方法的onClick()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在那里。我是一个新的Android开发人员和我的经验还是相当缺乏。我试图通过具有日志中每一个按钮被点击一次自我更新来更新GUI。我的问题是:为什么不日志#TextViews与更新的setText()时,P#TextViews会?我的code是如下:
公共类ZARP扩展活动
{
..
私人无效(P P,E)
{
的setContentView(R.layout.b);
TextView的P1 =(TextView中)findViewById(R.id.textView1);
TextView的P2 =(TextView中)findViewById(R.id.textView2);
TextView的P3 =(TextView中)findViewById(R.id.textView3);
TextView的P4 =(TextView中)findViewById(R.id.textView4);
TextView的P5 =(TextView中)findViewById(R.id.textView5);
TextView的P6 =(TextView中)findViewById(R.id.textView6);
p1.setText(你好); // WORKS
p2.setText(你好); // WORKS
p3.setText(你好); // WORKS
p4.setText(你好); // WORKS
p5.setText(你好); // WORKS
p6.setText(你好); // WORKS
..
按钮F =(按钮)findViewById(R.id.button2);
f.setOnClickListener(新OnClickListener()
{
公共无效的onClick(视图v)
{
的setContentView(R.layout.b);
TextView的LOG1 =(TextView中)findViewById(R.id.textView7);
TextView的LOG2 =(TextView中)findViewById(R.id.textView8);
TextView的log3中=(TextView中)findViewById(R.id.textView9);
TextView的LOG4 =(TextView中)findViewById(R.id.textView10);
log4.setText(log3.getText()); //不工作
log3.setText(log2.getText()); //不工作
log2.setText(log1.getText()); //不工作
log1.setText(你好); //不工作
}
...
}
和在XML为b.XML如下:
< TableLayout机器人:ID =@ + ID / tableLayout1机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT的xmlns:机器人=HTTP://模式.android.com / APK / RES / Android的>
<的TableRow机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / tableRow1机器人:baselineAligned =真正的>
< LinearLayout中的android:layout_weight =1机器人:layout_height =WRAP_CONTENT机器人:layout_width =WRAP_CONTENT机器人:ID =@ + ID / linearLayout6>
< ImageView的机器人:layout_weight =1机器人:layout_height =WRAP_CONTENT机器人:layout_width =WRAP_CONTENT机器人:ID =@ + ID / imageView1机器人:SRC =@可绘制/图标>< / ImageView的>
< / LinearLayout中>
<的LinearLayout机器人:ID =@ + ID / linearLayout1机器人:layout_height =WRAP_CONTENT机器人:layout_width =WRAP_CONTENT机器人:layout_weight =1>
< ImageView的机器人:layout_width =WRAP_CONTENT机器人:ID =@ + ID / imageView2机器人:layout_height =WRAP_CONTENT机器人:SRC =@可绘制/图标机器人:layout_gravity =权与GT;< / ImageView的>
< / LinearLayout中>
< /的TableRow>
<的LinearLayout机器人:ID =@ + ID / linearLayout2机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>
< TextView的机器人:ID =@ + ID / textView1机器人:文本=TextView的机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:layout_weight =1>< / TextView的>
< TextView的机器人:ID =@ + ID / textView4机器人:文本=TextView的机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:layout_weight =1机器人:layout_gravity =顶&GT ;< / TextView的>
< / LinearLayout中>
<的LinearLayout机器人:ID =@ + ID / linearLayout3机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>
< TextView的Android版本:文本=TextView的机器人:ID =@ + ID / textView2机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:layout_weight =1>< / TextView的>
< TextView的机器人:ID =@ + ID / textView5机器人:文本=TextView的机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:layout_weight =1机器人:layout_gravity =顶&GT ;< / TextView的>
< / LinearLayout中>
<的LinearLayout机器人:ID =@ + ID / linearLayout4机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>
< TextView的Android版本:文本=TextView的机器人:ID =@ + ID / textView3机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:layout_weight =1>< / TextView的>
< TextView的机器人:ID =@ + ID / textView6机器人:文本=TextView的机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:layout_weight =1机器人:layout_gravity =顶&GT ;< / TextView的>
< / LinearLayout中>
<按钮机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID /按钮2机器人:layout_width =WRAP_CONTENT机器人:文本=@字符串/ F>< /按钮>
<按钮机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / button1的机器人:layout_width =WRAP_CONTENT机器人:文本=@字符串/ R>< /按钮>
< LinearLayout中的android:layout_width =WRAP_CONTENT机器人:ID =@ + ID / linearLayout5机器人:layout_height =WRAP_CONTENT机器人:方向=垂直>
< TextView的Android版本:layout_height =WRAP_CONTENT机器人:layout_width =WRAP_CONTENT机器人:ID =@ + ID / textView10机器人:文本=TextView的>< / TextView的>
< TextView的Android版本:layout_height =WRAP_CONTENT机器人:layout_width =WRAP_CONTENT机器人:ID =@ + ID / textView9机器人:文本=TextView的>< / TextView的>
< TextView的Android版本:文本=TextView的机器人:ID =@ + ID / textView8机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< / TextView的>
< TextView的Android版本:文本=TextView的机器人:ID =@ + ID / textView7机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< / TextView的>
< / LinearLayout中>
< / TableLayout>
解决方案
删除的setContentView(R.layout.b);
从的onClick
和它的工作正如所料,对我来说。
there. I am a new Android developer, and my experience is still quite lacking. I'm trying to update the GUI by having a log that updates itself every time a button is clicked. My question is: Why don't the "log#" TextViews update with setText() when the "p#" TextViews will? My code is as follows:
public class ZARP extends Activity
{
..
private void b(P p, E)
{
setContentView(R.layout.b);
TextView p1 = (TextView)findViewById(R.id.textView1);
TextView p2 = (TextView)findViewById(R.id.textView2);
TextView p3 = (TextView)findViewById(R.id.textView3);
TextView p4 = (TextView)findViewById(R.id.textView4);
TextView p5 = (TextView)findViewById(R.id.textView5);
TextView p6 = (TextView)findViewById(R.id.textView6);
p1.setText("Hello");//WORKS
p2.setText("Hello");//WORKS
p3.setText("Hello");//WORKS
p4.setText("Hello");//WORKS
p5.setText("Hello");//WORKS
p6.setText("Hello");//WORKS
..
Button f = (Button)findViewById(R.id.button2);
f.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.b);
TextView log1 = (TextView)findViewById(R.id.textView7);
TextView log2 = (TextView)findViewById(R.id.textView8);
TextView log3 = (TextView)findViewById(R.id.textView9);
TextView log4 = (TextView)findViewById(R.id.textView10);
log4.setText(log3.getText());//DOESN'T WORK
log3.setText(log2.getText());//DOESN'T WORK
log2.setText(log1.getText());//DOESN'T WORK
log1.setText("Hello");//DOESN'T WORK
}
...
}
And the XML for the b.XML is as follows:
<TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tableRow1" android:baselineAligned="true">
<LinearLayout android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/linearLayout6">
<ImageView android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageView1" android:src="@drawable/icon"></ImageView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1">
<ImageView android:layout_width="wrap_content" android:id="@+id/imageView2" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_gravity="right"></ImageView>
</LinearLayout>
</TableRow>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="@+id/textView1" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
<TextView android:id="@+id/textView4" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="top"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
<TextView android:id="@+id/textView5" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="top"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></TextView>
<TextView android:id="@+id/textView6" android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="top"></TextView>
</LinearLayout>
<Button android:layout_height="wrap_content" android:id="@+id/button2" android:layout_width="wrap_content" android:text="@string/f"></Button>
<Button android:layout_height="wrap_content" android:id="@+id/button1" android:layout_width="wrap_content" android:text="@string/r"></Button>
<LinearLayout android:layout_width="wrap_content" android:id="@+id/linearLayout5" android:layout_height="wrap_content" android:orientation="vertical">
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView10" android:text="TextView"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView9" android:text="TextView"></TextView>
<TextView android:text="TextView" android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</TableLayout>
解决方案
Removed setContentView(R.layout.b);
from onClick
and it worked as expected, for me.
这篇关于TextView的方法的setText()不工作方法的onClick()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文