如何使用Android中的Textwatcher突出显示listview中的文本?

时间:2017-02-22 01:30:52

标签: android listview android-studio highlight android-textwatcher

我为editText创建了一个Textwatcher,用户可以在其中搜索文本文件中的项目。这些项目已拆分并列在列表视图中。它工作正常,但我想突出显示用户正在搜索的列表视图中的文本。 希望有人能帮帮我..

String[] items;
ArrayList<String> listItems;
ArrayAdapter<String> adapter;
ListView listView;
EditText editText;

private int mPreviousLength;
private boolean mBackSpace;

String text = "";


protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_code_view_eteile);

    listView=(ListView) findViewById(R.id.listview);

    editText=(EditText)findViewById(R.id.txtsearch);

    initList();

    editText.addTextChangedListener(new TextWatcher() {

        @Override

        public void beforeTextChanged(CharSequence s, int start, int count, int
                after)

            mPreviousLength = s.length();

        }

        @Override

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

            if(s.toString().equals("")){

                // reset listview

                initList();

            }

            else{

                // perform search

                searchItem(s.toString());
            }
        }

        @Override

        public void afterTextChanged(Editable s) {

            mBackSpace = mPreviousLength > s.length();

            if (mBackSpace) {

                initList();

            }

            searchItem(s.toString());

        }
    });
}


public void searchItem(String textToSearch){

    for(String item:items){

        if(!item.contains(textToSearch)){

            listItems.remove(item);

        }

    }

    adapter.notifyDataSetChanged();

}


public void initList(){
    try {
        InputStream is = getAssets().open("eteile_all.txt");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        text = new String(buffer);
    } catch (IOException ex) {
        ex.printStackTrace();

    }

    items = text.split(";");

    listItems=new ArrayList<>(Arrays.asList(items));

    adapter=new ArrayAdapter<String>(this,
            R.layout.list_codes, R.id.txtitem, listItems);

    listView.setAdapter(adapter);

}

activity_code_view_eteile.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">

<EditText
    android:id="@+id/txtsearch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapCharacters"
    android:imeOptions="actionDone"
    android:hint="Suche (E-Teil/Wartungskit)"
    />

<ListView
    android:id="@+id/listview"
    android:layout_below="@id/txtsearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

list_codes.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/txtitem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/colorPrimaryDark"
    android:padding="12dp"

    />

</LinearLayout>

0 个答案:

没有答案