安卓 - EditText 输入速度慢

7
我有一个EditText,打字时反应非常慢。这种延迟足以让我寻找解决方案。我进行了一些研究,并在SO线程EditText lagging when typing text中发现建议将代码移动到自己的线程中。我已经这样做了,但仍然遇到了延迟问题。
编辑:
在看到下面的评论后(感谢dreamtale),我知道新线程是不必要的。但是,将代码放回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>
</LinearLayout>

这是片段的代码:
```html

这是片段的代码:

```
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    try
    {
        vView = inflater.inflate(R.layout.submit_issue, container, false);

        //Setup initial "characters remaining" text.
        mCharsRemaining = (TextView)vView.findViewById(R.id.tvCharactersRemaining);
        mCharsRemaining.setText(String.valueOf(iMaxChars) + ' ' + getString(R.string.CharsRemaining));

        //Event for counting the characters remaining.
        mSummaryEditText.addTextChangedListener(TextEditorWatcher);

        new LoadPOCsTask().execute();   
    }
    catch (Exception e)
    {
        Errors.LogError(e);
    }
    return vView;           
}

文本编辑器监视器事件:
private final TextWatcher TextEditorWatcher = new TextWatcher() { 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 

        int iMaxCharsRemaining = (iMaxChars - s.length());
        mCharsRemaining.setText("Static Text "); //Intentionally put static text to see if problem still occurs, and yes it does.
    } 

    public void afterTextChanged(Editable s) { 
    } 
}; 

我不明白为什么打字时还会有延迟。如果我移除addTextChangeListener事件,那么它就能正常工作。你有什么想法吗?


EditTextWatcherTask任务 = new EditTextWatcherTask(s); task.execute(); 在afterTextChanged中调用... - Samir Mangroliya
仍然出现相同的延迟... - Robert
我认为你的任务并没有太大帮助,你所移动到后台的唯一事物是减法。至于实际的延迟,你是否尝试过确定导致延迟的确切原因?例如,尝试执行 mCharsRemaining.setText("静态文本" + mMaxCharsRemaining),以查看资源获取是否影响了性能。 - dmon
如果我在onTextChanged事件中放置静态文本(无论是“Static”还是“static”+String.valueof(mMaxCharsRemaining)),文本输入仍然非常缓慢。如果我不将EditText连接到监听器,则文本输入正常。 - Robert
5个回答

11

我曾在ListView中使用EditText时遇到类似的问题,通过将EditText的宽度设置为0dp并使用权重宽度以匹配或填充父容器来解决。

我不确定这种情况为什么会发生,但我认为原因是当EditText的宽度设置为自适应内容时,它会调整/重新绘制自身以适应所有内容;而ListView也会尝试重新绘制自身以适应所有内容。所以通过固定EditText的宽度,就不再需要重新绘制了。


这对我不起作用。您知道其他导致EditText滞后的原因吗? - Flame of udun

3
我发现关闭预测文本有帮助。
android:inputType="text|textNoSuggestions"

谢谢!帮我解决了三星设备的问题。 - Jaber Shabeek

2

编辑 AndroidManifest

我的旧代码

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" />

我的新代码

<uses-sdk android:minSdkVersion="8" />

我刚刚移除了targetSdkVersion,延迟问题就解决了...

0
当您输入文本时,onTextChanged 会一遍又一遍地被调用,因此您会一次又一次地启动一个新线程(EditTextWatcherTask),这将消耗系统的许多资源。
同时,线程是用于需要很长时间才能完成的任务,所以在您的情况下,您不需要线程,删除任务,只需将计算代码放在 onTextChanged 中即可。

谢谢回复...但我最初就有那段代码,而且响应非常慢。 - Robert
最初的意思是什么?无论如何,你都不应该在那里使用AsyncTask。移除任务,然后响应将正常。 - dreamtale
当我第一次创建代码时,它是在onTextChanged事件中(甚至尝试过afterTextChanged事件),但响应仍然很慢。因为你所说的话很有道理,我已经删除了Async Task。我之前只是按照另一篇帖子(在该线程中查看链接)的想法去尝试,似乎可以解决他们遇到的文本延迟问题。 - Robert
@Robert,我看到你提到的帖子了,也许他的用户界面很复杂或者有一些需要时间的操作。所以把耗时的工作放在线程中,然后再发布到用户界面上是可以的。但是在你的情况下,你不需要为了进行减法运算而创建一个线程。即使你需要一个线程,也不需要每次输入一个单词就新建一个任务。:( - dreamtale
@Robert 祝你好运,对于我重复的解释感到抱歉。 - dreamtale
显示剩余2条评论

0

我挣扎了一段时间,开始在xml中删除属性,最终得到了答案。在我的情况下,我删除了这些行:

        android:ellipsize="end"
        android:textAllCaps="true"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接