gpt4 book ai didi

android - setText() 和 append() 之间的区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:32 25 4
gpt4 key购买 nike

我很好奇 setText() 和 append() 所产生的差异。我正在编写一个带有行号的非常基本的编辑器。我有一个 TextView 来保存左侧的行号,与右侧的 EditText 配对以保存数据。这是 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top">
<TextView
android:id="@+id/line_numbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="0dip"
android:gravity="top"
android:textSize="14sp"
android:textColor="#000000"
android:typeface="monospace"
android:paddingLeft="0dp"/>
<EditText
android:id="@+id/editor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text|textMultiLine|textNoSuggestions"
android:imeOptions="actionNone"
android:gravity="top"
android:textSize="14sp"
android:textColor="#000000"
android:typeface="monospace"/>
</LinearLayout>

忽略我正在做的其他一些事情,我遇到的最奇怪的事情是当我使用 append() 时出现的额外间距(假设事情已经初始化等等)。

下面的内容与 XML 结合,在 TextView 和 EditText 之间设置了齐平的边框。

theEditor = (EditText) findViewById(R.id.editor);
lineNumbers = (TextView) findViewById(R.id.line_numbers);
theLineCount = theEditor.getLineCount();
lineNumbers.setText(String.valueOf(theLineCount)+"\n");

但是,将最后一行更改为此,TextView 中的每一行突然在 EditText 之前的右侧都有填充。

lineNumbers.append(String.valueOf(theLineCount)+"\n");

这不是世界末日。但我很好奇是什么导致了这种行为。由于我是该语言的新手,所以我唯一能想到的可能是,当 append 在那里抛出 Editable 时,它​​会添加填充。如果我能得到答案,我会用更简单的 append 内容替换所有这些讨厌的行:

lineNumbers.setText(lineNumbers.getText().toString()+String.valueOf(newLineCount)+"\n");

最佳答案

lineNumbers.setText("It is test,");

//Here lineNumbers have It is test

lineNumbers 将有“It is test,”。之后,如果再次使用 setText,text 将完全改变

lineNumbers.setText("It is second test,");

//Here you'll lose first text and lineNumbers text will be "It is second test,"

之后,如果你使用append,让我们看看会发生什么......

lineNumbers.append("It is third test,");

//这里你不会丢失 lineNumbers text.. 会是这样的“第二次考试,第三次考试”

关于android - setText() 和 append() 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19353992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com