机器人 - EditText上打字很慢 [英] android - EditText typing is slow

查看:133
本文介绍了机器人 - EditText上打字很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText是慢打字的时候作出回应。滞后是烦人足以引起我找到一个解决方案。我做了一些研究,发现了一个SO线程的EditText输入文字和它的时候落后建议对移动code到它自己的线程。我这样做,但我仍然遇到滞后。

I've got an EditText that is SLOW to respond when typing. The lag is annoying enough to cause me to find a solution. I did some research and found an SO thread EditText lagging when typing text and it suggests to move the code to it's own thread. I did that but I'm still experiencing the lag.

修改

在看到下面的意见(感谢dreamtale)后,我知道新的线程是没有必要的。但投入的onTextChanged或afterTextChanged事件中的code静止导致反应迟缓。我修改了code,以反映最新的变化:

After seeing the comments below (thanks dreamtale), I know the new thread isn't necessary. But putting the code back in the onTextChanged or afterTextChanged event still causes the slow response. I've modified the code to reflect the latest changes:

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>

这里的code的片段:

Here's the code for the fragment:

@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事件:

The TextEditorWatcher event:

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 事件,那么它工作正常。想法?

I can't figure out why it's still lagging when typing. If I remove the addTextChangeListener event, then it works fine. Ideas?

推荐答案

我是使用具有类似的问题的EditText A 的ListView里面,这是固定的,通过改变的EditText宽度 0dp 使用加权的宽度相匹配/填充母。

I was having a similar issue using EditText inside a ListView, that was fixed by changing the EditText width to 0dp using weighted widths to match/fill the parent.

我不知道的知道为什么,这是存在的,但是我相信这是因为当的的EditText 设置为包装的内容就会调整/重绘宽度本身使一切都适合,而的ListView 还会尝试重绘本身,以便一切都适合。因此,通过使的EditText 有一个固定的宽度,这重绘不再需要。

I don't know for sure why this was occurring, however I believe it is because when the width of the EditText is set to wrap content it will adjust/redraw itself so that everything fits, and the ListView will also attempt to redraw itself so everything fits. So by making the EditText have a fixed width, this redraw is no longer required.

这篇关于机器人 - EditText上打字很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆