使用PopupWindow弹窗实现textview自动完成功能问题

bamboo_zhang 2015-05-14 06:02:45
求给位大神解答,小妹已经折腾了快两个星期了

先上图:


目前出现的问题就是,只能输入一个字母,然后就无法输入的,感觉快崩溃了
一开始在网上看见一个例子 ,http://blog.csdn.net/withiter/article/details/11843843,但是addView之后无法移除才换的这个
textview 代码:

//自动搜索联系人 @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

}

});


使用PopupWindow 代码:

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);
}
}


求各位做过类似程序的大神解答,本人刚学Android一个多月,苦不堪言啊
另外老号丢失,新号分不多,各位不要介意咯
...全文
386 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
DRPrincess 2016-05-16
  • 打赏
  • 举报
回复
楼主,将PopupWhindow.setFocusable(false)后editText获取不到焦点了 楼主,我在实现popwindow +AutoCompleteTextView时遇到问题,popwindow弹出正常,但是AutoCompleteTextView检索时会崩溃,你遇到过吗?能给我提点建议吗 错误日志是 05-16 12:25:49.580 32475-32475/dianshi.matchtrader E/AndroidRuntime: FATAL EXCEPTION: main Process: dianshi.matchtrader, PID: 32475 android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@4337eac0 is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:594) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.widget.PopupWindow.invokePopup(PopupWindow.java:1025) at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:928) at android.widget.ListPopupWindow.show(ListPopupWindow.java:668) at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1096) at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:971) at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:953) at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5314) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680) at dalvik.system.NativeStart.main(Native Method)
bamboo_zhang 2015-05-26
  • 打赏
  • 举报
回复
我自己回答吧,休息了一个星期,头脑清醒了不少,终于把问题搞定了。 这个地方改为false searchePop.setFocusable(false); 同时为输入控件重新获取焦点就ok啦,等过几天这段忙完了来上传代码结贴
bamboo_zhang 2015-05-25
  • 打赏
  • 举报
回复
导致这个问题出现的原因应该是pop弹出后背景层失去了焦点,继续,0基础就是痛苦啊
bamboo_zhang 2015-05-24
  • 打赏
  • 举报
回复
引用 1 楼 u010695063 的回复:
文章太长没看,你这个效果应该用AutoCompleteView来写,不知道单词拼错没有,这个控件是用来做输入信息,然后自动匹配的。如:妻子、父亲等 可以在XML该控件中用hint属性里写出来
这个星期有事 ,都没有上来。你说的那个,应该是AutoCompleteTextView 一开始试过,折腾了很久,因为要匹配的东西出自数据库,所以网上的资料比较少, 折腾不出来,只好放弃。才想着用其他方法实现的。 感觉Android都出来好几年了,这网上的资料还是这么少,百度的资料基本都是2012年前的,郁闷,继续一边研究一边等大神
  • 打赏
  • 举报
回复
文章太长没看,你这个效果应该用AutoCompleteView来写,不知道单词拼错没有,这个控件是用来做输入信息,然后自动匹配的。如:妻子、父亲等 可以在XML该控件中用hint属性里写出来

80,466

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧