目前,在处理大量文本行(10000+)时,Android的EditText非常慢.看起来这种减速部分是由于EditText支持跨度,主要是因为EditText正在计算每条线的宽度,这是非常昂贵的.是否有更快的EditText替代品,或者一种优化它以使其可用的方法?
编辑:方法跟踪如下:
android.text.StaticLayout.generate: 99.1% CPU time inclusive, 8.8% exclusive (1 call)
android.text.Layout.getParagraphSpans: 28% inclusive, 1.1% exclusive (4686 calls)
android.text.MeasuredText.setPara: 20.6% inclusive, 1.6% exclusive (2343 calls)
android.text.MeasuredText.addStyleRun: 18.6% inclusive, 1.1& exclusive (2343 calls)
android.text.SpannableStringBuilder.getSpans: 15% inclusive (of parent calls), 56.7% inclusive (of all calls, 47.3% of which are from android.text.Layout.getParagraphSpans, 26% are from android.text.MeasuredText.setPara, 26% are from android.text.StaticLayout.generate)
Run Code Online (Sandbox Code Playgroud) 我有一个EditText,在输入时响应缓慢.滞后很烦人,足以让我找到解决方案.我做了一些研究,发现一个SO线程EditText在键入文本时滞后 ,它建议将代码移动到它自己的线程.我做到了,但我仍然遇到了滞后.
编辑
看到下面的评论(感谢梦想),我知道新线程是没有必要的.但是将代码放回onTextChanged或afterTextChanged事件仍会导致响应缓慢.我修改了代码以反映最新的更改:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/right_layout_header" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:drawable="@drawable/light_bg" >
<TableRow
android:layout_width="200dp"
android:layout_height="wrap_content"
android:shrinkColumns="1">
<TextView
android:id="@+id/tvSummaryBold"
android:layout_width="wrap_content"
android:textStyle="bold"
android:gravity="left"
android:text="@string/Summary"
style="@style/IssueDetailsLabelTextView" />
</TableRow>
<TableRow
android:layout_width="200dp"
android:layout_height="wrap_content"
android:shrinkColumns="1">
<EditText
android:id="@+id/txtSummary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/SummaryDefaultText"
android:maxLength="255"
android:singleLine="true"
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/tvCharactersRemaining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Gray"
android:paddingLeft="5dp"
/>
</TableRow>
</TableLayout>
</ScrollView> …Run Code Online (Sandbox Code Playgroud)