简体   繁体   English

如何更改屏幕上的textView位置以及如何更改文本?

[英]How can i change a textView position on screen and how to change it's text?

This is the textView2 code in my activity_main.xml 这是我的activity_main.xml中的textView2代码

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Checking Connection.."
        android:layout_above="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="47dp"/>

In the designer the textView2 is in the middle of the screen not in the center a bit upper from the center but in the middle. 在设计器中,textView2位于屏幕的中间,而不是位于中心上方,但位于中心的中间位置。

Now i'm trying to change it's text in a real time inside a FOR loop: 现在,我正在尝试在FOR循环中实时更改其文本:

public void addListenerOnButton()
    {

        btnClick = (Button) findViewById(R.id.checkipbutton);

        btnClick.setOnClickListener(new OnClickListener()
        {
            byte[] response = null;
            @Override
            public void onClick(View arg0)
            {

                text = (TextView) findViewById(R.id.textView2);

                Thread t = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        for (int i = 0; i < ipaddresses.length; i++)

                        {

                                try
                                {
                                    text.post(new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {
                                            text.setText("Checking Connection With Ip: " + ipaddresses[i]);
                                        }
                                    });
                                    response = Get(ipaddresses[i]);
                                }
                                catch (Exception e)
                                {
                                    String err = e.toString();
                                }

                                if (response!=null)
                                {



                                    try
                                    {
                                        final String a = new String(response,"UTF-8");



                                        text.post(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                text.setText(a);
                                            }
                                        });

                                        Logger.getLogger("MainActivity(inside thread)").info(a);
                                    } catch (UnsupportedEncodingException e)
                                    {
                                        e.printStackTrace();
                                        Logger.getLogger("MainActivity(inside thread)").info("encoding exception");
                                    }

                                    Logger.getLogger("MainActivity(inside thread)").info("test1");
                                    break;

                                }

                                else
                                {

                                }





                        }
                        if (response == null)
                        {
                            text.post(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    text.setText("Connection Failed");
                                }
                            });
                        }
                    }
                });
                t.start();
            }
        });

    }

The part i added now is: 我现在添加的部分是:

text.post(new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {
                                            text.setText("Checking Connection With Ip: " + ipaddresses[i]);
                                        }
                                    });

Two problems: 两个问题:

When i change here the text of textView2 i need to move textView2 to the left so all the text will be inside the screen. 当我在此处更改textView2的文本时,我需要将textView2移至左侧,以便所有文本都在屏幕内。

Second problem is that it dosen't identify the variabl: i 第二个问题是它无法识别变量:

To move the TextView, you have to do this: 要移动TextView,您必须执行以下操作:

RelativeLayout.LayoutParams params = 
    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView.setLayoutParams(params);

and to access the i variable, you have to declare it final. 并访问i变量,您必须将其声明为final。

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

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