Android EditText alternative

2019-02-08 00:53发布

Currently, Android's EditText is extremely slow when dealing with a huge amount of lines of text (10000+). It appears like this slowdown is partially due to the fact that EditText supports spans, and primarily due to the fact that EditText is calculating the width of each line, which is very expensive. Are there any faster alternatives to EditText, or a way to optimize it to make it usable?

EDIT: Method traces are as follows:

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)

4条回答
甜甜的少女心
2楼-- · 2019-02-08 01:17

The best thing you can do is using RecyclerView with EditText as its item, so you get a new EditText for each of your lines.

New line will be the only thing you will have to implement.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-08 01:30

The alternative would be to use a TextView (how else would you show the text) and make it act as a EditText. Here's how:

You'll set an OnClickListener on your TextView, to show the keyboard:

textView.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        isEnteringText = true; //global
    }
});

And override onKeyDown and process keyboard presses to the TextView:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(isEnteringText) {
        textView.append(event.getDisplayLabel());
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Obviously this will need a lot of work, such as hiding the keyboard afterward, processing backspace & enter, and clipboard. I kinda formed my answer around this post, you can try the other methods mentioned in there if you're having problems retrieving the keyboard keys. According to them, the above code will work as long as the language is English.

查看更多
放荡不羁爱自由
4楼-- · 2019-02-08 01:33

Although this article only talks about optimizing static TextViews where the text doesn't change, it might get you on the right track for making a more performant EditText.

查看更多
Deceive 欺骗
5楼-- · 2019-02-08 01:41

Avoid using EditText inside a RelativeLayout, use LinearLayout instead.

查看更多
登录 后发表回答