【问题标题】:android - EditText typing is slowandroid - EditText 打字很慢
【发布时间】:2012-03-27 14:43:55
【问题描述】:

我有一个 EditText,打字时响应速度很慢。滞后很烦人,足以让我找到解决方案。我做了一些研究,发现了一个 SO 线程EditText lagging when typing text,它建议将代码移动到它自己的线程。我这样做了,但我仍然遇到滞后。

编辑

在看到下面的 cmets 之后(感谢 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>

这是片段的代码:

@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;           
}

TextEditorWatcher 事件:

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 任务 = 新的 EditTextWatcherTask(s);任务.执行(); call in afterTextChanged...
  • 仍然有同样的滞后...
  • 我不认为你的任务有多大帮助,你唯一要转移到后台的是减法。至于实际的滞后,您是否尝试过查明导致滞后的确切原因?例如,尝试执行 mCharsRemaining.setText("static text" + mMaxCharsRemaining) 以查看资源获取是否会影响性能。
  • 如果我将静态文本放在 onTextChanged 事件中(“静态”或“静态”+ String.valueof(mMaxCharsRemaining) ),文本输入仍然很慢。如果我不将 EditText 连接到侦听器,那么文本输入就可以了。

标签: android android-asynctask


【解决方案1】:

我在ListView 中使用EditText 时遇到了类似的问题,通过使用加权宽度匹配/填充父级将EditText width 更改为0dp 已解决此问题。

我不确定为什么会发生这种情况,但我相信这是因为当 EditText 的宽度设置为包裹内容时,它会自行调整/重绘以使一切都适合,而 ListView还将尝试重新绘制自己,以便一切都适合。因此,通过使EditText 具有固定宽度,不再需要重新绘制。

【讨论】:

  • 这对我不起作用。你知道edittext会滞后的其他原因吗?
【解决方案2】:

我发现关闭预测文本很有帮助。

android:inputType="text|textNoSuggestions"

【讨论】:

  • 谢谢..!帮助我使用三星设备
【解决方案3】:

编辑AndroidManifest:

我的旧代码

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

我的新代码

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

我刚刚删除了 targetSdkVersion 并且延迟消失了...

【讨论】:

    【解决方案4】:

    当您输入文本时,onTextChanged 会一次又一次地被调用,因此您会一次又一次地开始一个新线程 (EditTextWatcherTask)。它会消耗系统的许多资源。

    线程是用于需要很多时间才能完成的任务,所以在你的情况下,你不需要线程,删除任务,只需将计算代码放在onTextChanged中。

    【讨论】:

    • 感谢您的回复...但我原来有代码,响应速度很慢。
    • 原来是什么意思?无论如何你不应该在那里使用AsyncTask。删除任务,然后响应将正常。
    • 我第一次创建代码时,是在onTextChanged事件中(甚至尝试了afterTextChanged事件),响应仍然很慢。由于您所说的,我确实删除了异步任务,这是有道理的。我只是因为另一个帖子而放弃了这个想法(请参阅线程中的链接),它似乎解决了他们关于滞后文本的问题。
    • @Robert 我看过你注意到的帖子,也许他的 UI 很复杂,或者有一些事情需要一些时间。所以把耗时的工作放到线程里再post到UI上就OK了。但是在您的情况下,您不需要线程来计算减法。即使你需要一个线程,你不需要每次输入一个单词时都需要 new Task :(
    • 我从您的原始回答中了解到这一点。我将编辑帖子以反映这一点,希望我能找到解决方案。
    【解决方案5】:
    android:vmSafeMode="true"
    

    从清单中删除此属性将起作用。它对我有用。

    【讨论】:

      猜你喜欢
      • 2021-03-29
      • 1970-01-01
      • 2015-09-06
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多