简体   繁体   English

TextView方法setText()在方法onClick()中不起作用

[英]TextView method setText() not working in method onClick()

there. 那里。 I am a new Android developer, and my experience is still quite lacking. 我是一名新的Android开发人员,但我的经验仍然很缺乏。 I'm trying to update the GUI by having a log that updates itself every time a button is clicked. 我正在尝试通过使日志在每次单击按钮时进行更新来更新GUI。 My question is: Why don't the "log#" TextViews update with setText() when the "p#" TextViews will? 我的问题是:为什么当“ p#” TextViews会更新时,为什么不使用setText()更新“ log#” TextViews? 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: b.XML的XML如下:

<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); 删除了setContentView(R.layout.b); from onClick and it worked as expected, for me. 来自onClick ,它对我来说按预期工作。

Try removing the call setContentView(xxxxxx) in the OnClick method. 尝试在OnClick方法中删除调用setContentView(xxxxxx)。 You already set the layout. 您已经设置了布局。 By calling it again all the listeners for the buttons (maybe even the handles for the textViews, not sure) are removed. 通过再次调用它,将删除按钮的所有侦听器(甚至不确定textViews的句柄)。

Removed setContentView(R.layout.b); 删除了setContentView(R.layout.b); You are repeating the same thing! 您在重复同一件事!

public class ZARP extends Activity
{
       setContentView(R.layout.b);
      ...

         private void b(P p, E)
             {..........

add setContentView(R.layout.b); 添加setContentView(R.layout.b); like the above and try. 像上面一样尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM