Android EditText with TextWatcher (從ListView中搜索數據)含代碼

    Android EditText with TextWatcher (從ListView中搜索數據)

    Android EditText 是TextView的子類。愛掏網 - it200.comEditText用于輸入和修改文本。愛掏網 - it200.com在使用EditText的寬度時,我們必須在EditText的inputType屬性中指定其輸入類型,根據輸入配置鍵盤。愛掏網 - it200.com

    EditText使用 TextWatcher 接口來監聽對EditText的更改。愛掏網 - it200.com為此,EditText調用 addTextChangedListener() 方法。愛掏網 - it200.com

    TextWatcher的方法

    1. beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3): 在對EditText進行任何更改之前執行。愛掏網 - it200.com
    2. onTextChanged(CharSequence cs, int arg1, int arg2, int arg3): 在對EditText進行任何更改時執行。愛掏網 - it200.com
    3. afterTextChanged(Editable arg0): 在對EditText進行更改后執行。愛掏網 - it200.com

    EditText與TextWatcher()的示例

    在這個示例中,我們將使用EditText和TextWatcher來從ListView中搜索數據。愛掏網 - it200.com

    activity_main.xml

    在layout文件夾中創建一個activity_main.xml文件,其中包含EditText和ListView。愛掏網 - it200.com

    文件:activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <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="com.example.test.searchfromlistview.MainActivity">  
    
        <EditText  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:id="@+id/editText"  
            android:inputType="text"  
            android:layout_alignParentTop="true"  
            android:layout_alignParentLeft="true"  
            android:layout_alignParentStart="true"  
            android:layout_alignParentRight="true"  
            android:layout_alignParentEnd="true" />  
    
        <ListView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:id="@+id/listView"  
            android:layout_below="@+id/editText"  
            android:layout_alignParentLeft="true"  
            android:layout_alignParentStart="true" />  
    </RelativeLayout>  
    

    在layout文件夾中創建另一個名為list_item.xml的文件,其中包含ListView的數據。愛掏網 - it200.com

    list_item.xm

    文件:list_item.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <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/product_name"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:padding="10dip"  
        android:textSize="16dip"  
        android:textStyle="bold"/>  
    </LinearLayout>  
    

    Activity類

    Activity類

    package com.example.test.searchfromlistview;
    
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private ListView lv;
        private EditText editText;
        private ArrayAdapter adapter;
    
        private String products[] = {"Apple", "Banana","Pinapple", "Orange", "Papaya", "Melon",
                "Grapes", "Water Melon","Lychee", "Guava", "Mango", "Kivi"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            lv = (ListView) findViewById(R.id.listView);
            editText = (EditText) findViewById(R.id.editText);
            adapter = new ArrayAdapter(this, R.layout.list_item, R.id.product_name, products);
            lv.setAdapter(adapter);
    
            editText.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    adapter.getFilter().filter(cs);
                }
    
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    Toast.makeText(getApplicationContext(),"before text change",Toast.LENGTH_LONG).show();
                }
    
                @Override
                public void afterTextChanged(Editable arg0) {
                    Toast.makeText(getApplicationContext(),"after text change",Toast.LENGTH_LONG).show();
                }
            });
        }
    }
    

    輸出

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 日本一区二区在线不卡| 日本伊人精品一区二区三区| 国产福利一区二区三区视频在线| 日韩免费视频一区| 搡老熟女老女人一区二区| 国产一区二区三区视频在线观看| 99精品国产一区二区三区不卡| 三上悠亚精品一区二区久久| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 一区二区亚洲精品精华液| 精品亚洲综合在线第一区| 美女视频一区二区| 狠狠爱无码一区二区三区| 波多野结衣中文一区| 视频一区二区三区免费观看| 国产成人精品视频一区| 成人一区二区三区视频在线观看| 亚洲AV无码一区二区三区人| 精品无码日韩一区二区三区不卡| 无码国产精品一区二区免费式芒果| 中文字幕久久亚洲一区| 日本一区午夜艳熟免费| 国产精品毛片一区二区三区| 国产一区二区内射最近更新| 手机看片福利一区二区三区| 亚洲成a人一区二区三区| 国产精品区AV一区二区| 国产日韩精品一区二区三区在线 | 亚洲无线码一区二区三区| 国产一区在线视频| 无码人妻精品一区二区三区东京热| 亚洲一区二区三区自拍公司| 一区二区三区在线|欧| 中文字幕AV一区二区三区人妻少妇| 亚洲一区二区三区写真| 福利片福利一区二区三区| 亚洲AV无码一区二区三区系列| 国产伦精品一区二区三区| 久久青草精品一区二区三区| 亚洲一区二区三区在线| 精品人妻少妇一区二区三区不卡 |