你的位置:首页 > 操作系统

[操作系统]edittext 监听内容变化


给EditText追加ChangedListener可以监听EditText内容变化的监听

如图是效果图  类似于过滤的一种实现

1  布局也就是一个EditText,当EditText内容发生变化时(当输入数字时)就会出现一个PopupWindow通过ChangedListener来显示监听

2 数据是进入这个页面就加载出来的,当输入的内容变化时,通过遍历改变

3 字体颜色的变化http://www.cnblogs.com/sinber/archive/2011/10/27/2227012.html

如下是主要代码

package com.it.hello.activity.fragment;import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.xutils.x;import org.xutils.common.Callback;import org.xutils.http.RequestParams;import com.it.hello.R;import com.it.hello.activity.adapter.GetPhoneAdapter;import android.graphics.Color;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.support.v4.app.Fragment;import android.text.Editable;import android.text.SpannableString;import android.text.Spanned;import android.text.TextWatcher;import android.text.style.ForegroundColorSpan;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.EditText;import android.widget.LinearLayout.LayoutParams;import android.widget.ListView;import android.widget.PopupWindow;/** * 主要实现了一些edit的功能 * * @author Administrator * */public class MOneFragment extends Fragment {  private List<String> allList = new ArrayList<String>();  private List<SpannableString> phoneList2 = new ArrayList<SpannableString>();  public static EditText edt_Text;  public static PopupWindow popupwindow;  String usernameString, phoneString;  //private boolean isFinished = true;  //private int total = 0;  //private int pageSize = 100;  private GetPhoneAdapter adapter;  //private int tatalPage = 1;  private String urlPhone = "http://111.39.245.157:9527/cmppbs/getCmpp30PhoneList.action";  @Override  public View onCreateView(LayoutInflater inflater, ViewGroup container,      Bundle savedInstanceState) {    // TODO Auto-generated method stub    return inflater.inflate(R.layout.monefragment, container, false);  }  @Override  public void onActivityCreated(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onActivityCreated(savedInstanceState);    setView();  }  private void setView() {    // TODO Auto-generated method stub    edt_Text = (EditText) getActivity().findViewById(R.id.edt_number);    setHttp();    adapter = new GetPhoneAdapter(getActivity(), phoneList2);    edt_Text.addTextChangedListener(new TextWatcher() {      @Override      public void onTextChanged(CharSequence s, int start, int before,          int count) {        // TODO Auto-generated method stub        if (s.length() > 0) {          View custstomView = getActivity().getLayoutInflater()              .inflate(R.layout.popview_item, null, false);          ListView listView = (ListView) custstomView              .findViewById(R.id.listView_select);          if (popupwindow == null) {            popupwindow = new PopupWindow(custstomView, edt_Text                .getWidth(), LayoutParams.WRAP_CONTENT, true);                      }          phoneList2.clear();          if (s.length() > 0) {            for (String str : allList) {              if (str.startsWith("" + s)) {                SpannableString ss = new SpannableString(str);                ss.setSpan(new ForegroundColorSpan(Color.RED),                    0, s.length(),                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);                phoneList2.add(ss);              }            }          } else {            for (String str : allList) {              SpannableString ss = new SpannableString(str);              phoneList2.add(ss);            }          }          if (phoneList2.size() == 0) {            phoneList2.add(new SpannableString("没找到号码"));          }          adapter.notifyDataSetChanged();          listView.setAdapter(adapter);          popupwindow.setOutsideTouchable(true);          popupwindow.setFocusable(false);          popupwindow.setBackgroundDrawable(new BitmapDrawable());          popupwindow.showAsDropDown(edt_Text, 0, 0);        }      }      @Override      public void beforeTextChanged(CharSequence arg0, int arg1,          int arg2, int arg3) {        // TODO Auto-generated method stub      }      @Override      public void afterTextChanged(Editable arg0) {        // TODO Auto-generated method stub      }    });  }  /**   * 进行网络请求 获取得到电话的数据   */  private void setHttp() {    // TODO Auto-generated method stub    RequestParams params = new RequestParams(urlPhone);    params.addBodyParameter("page", "" + 0);    params.addBodyParameter("rows", "" + 100);    x.http().get(params, new Callback.CommonCallback<String>() {      @Override      public void onCancelled(CancelledException arg0) {        // TODO Auto-generated method stub      }      @Override      public void onError(Throwable arg0, boolean arg1) {        // TODO Auto-generated method stub      }      @Override      public void onFinished() {        // TODO Auto-generated method stub      }      @Override      public void onSuccess(String arg0) {        // TODO Auto-generated method stub        Log.d("jiejie", arg0);        if (arg0 != null) {          try {            JSONObject object = new JSONObject(arg0);            JSONArray array = object.getJSONArray("rows");//            total = object.getInt("total");//            tatalPage = (total + pageSize - 1) / pageSize;            for (int i = 0; i < array.length(); i++) {              usernameString = array.getJSONObject(i).getString(                  "userName");              phoneString = array.getJSONObject(i).getString(                  "terminalId");              allList.add(phoneString + " " + usernameString);            }          } catch (JSONException e) {            // TODO Auto-generated catch block            e.printStackTrace();          }          //isFinished = true;        }      }    });  }}

<?  android:layout_width="match_parent"  android:layout_height="match_parent"  android:focusable="true"  android:focusableInTouchMode="true"  android:orientation="vertical" >  <EditText    android:id="@+id/edt_number"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_margin="20dp"    android:background="@drawable/textview_bg"    android:hint="请填写号码"    android:inputType="number"    android:singleLine="true" /></LinearLayout>