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 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 日本道精品一区二区三区| 色综合天天综一个色天天综合网| 精品小视频在线| 无码人妻精品一区二区三18禁| 国产成人免费片在线观看| 五月激情婷婷网| 国产90后美女露脸在线观看| 日韩视频免费在线播放| 国产成人女人在线观看| 久久精品国产屋| 野花日本免费观看高清电影8 | 欧美性xxxxx极品娇小| 国产高清www免费视频| 亚洲欧美日韩精品在线| 69国产成人精品午夜福中文| 欧美日韩国产在线人成| 国产精品无码一区二区三区在| 亚洲国产成人精品激情| 中文字幕天天干| 日韩人妻精品一区二区三区视频 | 你懂得的在线观看免费视频| caopon在线| 污视频免费网站| 国产精品林美惠子在线播放| 亚洲人成精品久久久久| 97成人在线视频| 日产精品99久久久久久| 午夜福利啪啪片| V一区无码内射国产| 欧美性猛交xxx黑人猛交| 国产欧美va欧美va香蕉在| 久久亚洲精品无码观看不卡| 美美女高清毛片视频免费观看 | 国产性夜夜夜春夜夜爽| 久久久久性色AV毛片特级| 精品深夜av无码一区二区| 天天影院成人免费观看| 亚洲欧洲中文日产| 国产成人在线网址| 成人看片黄a在线观看| 人妻少妇精品视频一区二区三区|