80,466
社区成员




//自动搜索联系人 @SuppressWarnings("unchecked")
crp_txtConName.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable str) {
// TODO Auto-generated method stub
System.out.println("上次输入:"+oldText+"本次输入:"+str.toString());
if(str.length()>0&&oldText!=str.toString()){
oldText=str.toString();
//createView(str.toString());
System.out.println("进入:createPopup");
createPopup(str.toString());
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
private void createPopup(String str){
System.out.println("isFirstLoad:"+isFirstLoad);
//if(isFirstLoad){
//获取LayoutInflater实例
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
//这里的main布局是在inflate中加入的哦,以前都是直接this.setContentView()的吧?呵呵
//该方法返回的是一个View的对象,是布局中的根
View layout = inflater.inflate(R.layout.con_float_list, null);
//下面我们要考虑了,我怎样将我的layout加入到PopupWindow中呢???很简单
searchePop = new PopupWindow(layout,crp_txtConName.getWidth(), LayoutParams.WRAP_CONTENT); //后两个参数是width和height
//searchePop.showAsDropDown(layout); //设置弹出效果
//searchePop.showAsDropDown(null, 0, layout.getHeight());
//设置如下四条信息,当点击其他区域使其隐藏,要在show之前配置
searchePop.setFocusable(true);
searchePop.setOutsideTouchable(true);
searchePop.update();
searchePop.setBackgroundDrawable(new BitmapDrawable()); //当点击其他区域使其隐藏
//如何获取我们main中的控件呢?也很简单
con_float_Lst= (ListView)layout.findViewById(R.id.con_float_Lst);
SQLiteDatabase nameDb=openDatabase();
cur=nameDb.rawQuery("select ContactID as _id,ContactName,NickName from"
+" Contact where SortKey like '%"+str+"%' or SimpleSpelling like '%"+str+"%'",
null);
System.out.println("联系人读取条数:"+cur.getCount());
seachAdapter=new ConSearchAdapter(ConRelationPop.this,cur,R.layout.item_con_name_list);
con_float_Lst.setAdapter(seachAdapter);
con_float_Lst.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int index, long id) {
// TODO Auto-generated method stub
System.out.println("con_float_Lst.OnItemClickListener,isFirstLoad"+isFirstLoad);
cur.moveToPosition(index);
crp_txtConName.setText(cur.getString(cur.getColumnIndex("ContactName")));
searchePop.dismiss();
isFirstLoad=true;
System.out.println(isFirstLoad);
}});
//searchePop.showAtLocation(crp_txtConName, Gravity.LEFT|Gravity.BOTTOM, 0,0); //设置layout在PopupWindow中显示的位置
searchePop.showAsDropDown(crp_txtConName);
isFirstLoad=false;
/* }else{
SQLiteDatabase nameDb=openDatabase();
cur=nameDb.rawQuery("select ContactID as _id,ContactName,NickName from"
+" Contact where SortKey like '%"+str+"%' or SimpleSpelling like '%"+str+"%'",
null);
System.out.println("联系人读取条数:"+cur.getCount());
seachAdapter=new ConSearchAdapter(ConRelationPop.this,cur,R.layout.item_con_name_list);
con_float_Lst.setAdapter(seachAdapter);
searchePop.update();
}*/
}
private void createView(String str) {
// 获取WindowManager ConRelationPop
//wm = (WindowManager) getApplicationContext().getSystemService("window");
wm = (WindowManager) ConRelationPop.this.getSystemService("window");
WindowManager.LayoutParams wmParams =new WindowManager.LayoutParams();
//WindowManager.LayoutParams wmParams =((AppApplication) getApplication()).getWmParams();
int[] location = new int[2];
crp_txtConName.getLocationOnScreen(location);
//searchResultView = new CommonFloatView(getApplicationContext());
searchResultView = new CommonFloatView(ConRelationPop.this);
searchResultView.setBackgroundColor(Color.RED);
searchResultView.getBackground().setAlpha(80);
searchResultView.setVerticalScrollBarEnabled(true);
SQLiteDatabase nameDb=openDatabase();
cur=nameDb.rawQuery("select ContactID as _id,ContactName,NickName from"
+" Contact where SortKey like '%"+str+"%' or SimpleSpelling like '%"+str+"%'",
null);
System.out.println("联系人读取条数:"+cur.getCount());
seachAdapter=new ConSearchAdapter(ConRelationPop.this,cur,R.layout.item_con_name_list);
searchResultView.setAdapter(seachAdapter);
searchResultView.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int index, long arg3) {
// TODO Auto-generated method stub
cur.moveToPosition(index);
crp_txtConName.setText(cur.getString(cur.getColumnIndex("ContactName")));
System.out.println("searchResultView的父窗体:"+searchResultView.getParent().toString());
if(null!=searchResultView.getParent()){
wm.removeView(searchResultView);
isFirstLoad=true;
}
/*cur.getString(cur.getColumnIndex("_id"))+";"
+cur.getString(cur.getColumnIndex("ContactName"))+";"
+cur.getString(cur.getColumnIndex("NickName"));*/
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
// 设置LayoutParams(全局变量)相关参数
//wmParams.type = LayoutParams.MATCH_PARENT; // 设置window type
//wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明
wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 调整悬浮窗口至左上角
//设置x、y初始值
wmParams.x = location[0];
wmParams.y = location[1];
// 设置悬浮窗口长宽数据
wmParams.width = 120;
wmParams.height = LayoutParams.WRAP_CONTENT;
// 显示myFloatView图像
if(isFirstLoad){
wm.addView(searchResultView, wmParams);
isFirstLoad=false;
}else{
wm.updateViewLayout(searchResultView, wmParams);
}
}