本文介绍了自动完成的TextView在PopupWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我显示按钮PopupWindow点击这个样子。
公共无效Search_Click(查看视图){
尝试
{
显示显示= getWindowManager()getDefaultDisplay()。
LayoutInflater充气=(LayoutInflater)IssueTokenActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看布局= inflater.inflate(R.layout.activity_pop_up_transporter_details,
(ViewGroup中)findViewById(R.id.popup_element));
AutoCompleteTextView行为=(AutoCompleteTextView)layout.findViewById(R.id.act_trans_name);
ArrayAdapter<字符串> DataAdapter的=新的ArrayAdapter<字符串>(view.getContext(),android.R.layout.simple_dropdown_item_1line,清单);
dataadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
act.setAdapter(DataAdapter的);
act.setThreshold(1);
// TODO:需要更高的API支持
pwindo =新PopupWindow(布局,display.getWidth() - 60,display.getHeight() - 400,真正的);
pwindo.showAtLocation(布局,Gravity.CENTER,0,0);
btnClosePopup =(按钮)layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
}
赶上(例外五)
{
e.printStackTrace();
}
}
不过,我收到此错误而自动完成TextView中键入。
九月6号至20号:15:47.091:E / AndroidRuntime(23277):android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.view.ViewRootImpl $ W @ 40ece840无效;在您的活动运行?
9月六号至20号:15:47.091:E / AndroidRuntime(23277):在android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
9月六号至20号:15:47.091:E / AndroidRuntime(23277):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
9月六号至20号:15:47.091:E / AndroidRuntime(23277):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
9月六号至20号:15:47.091:E / AndroidRuntime(23277):在android.widget.PopupWindow.invokePopup(PopupWindow.java:993)
9月六号至20号:15:47.091:E / AndroidRuntime(23277):在android.widget.PopupWindow.showAsDropDown(PopupWindow.java:899)
9月六号至20号:15:47.091:E / AndroidRuntime(23277):在android.widget.ListPopupWindow.show(ListPopupWindow.java:603)
解决方案
最好使用对话框(android.app.Dialog)实施AutoCompleteTextView.In它不可能我看来添加AutoCompleteTextView在PopupWindow(你会得到例外)。您可以在Popupwindow.You添加微调如果两者都使用对话框而不是弹出式可以实现。
I am showing PopupWindow on button click like this.
public void Search_Click(View view) {
try
{
Display display=getWindowManager().getDefaultDisplay();
LayoutInflater inflater = (LayoutInflater) IssueTokenActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_pop_up_transporter_details,
(ViewGroup) findViewById(R.id.popup_element));
AutoCompleteTextView act=(AutoCompleteTextView)layout.findViewById(R.id.act_trans_name);
ArrayAdapter<String> dataadapter=new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_dropdown_item_1line,list);
dataadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
act.setAdapter(dataadapter);
act.setThreshold(1);
//TODO: Need to support for higher API
pwindo = new PopupWindow(layout,display.getWidth()-60, display.getHeight()-400, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
}
catch (Exception e)
{
e.printStackTrace();
}
}
But I am getting this error while typing in AutoCompleted TextView.
06-20 09:15:47.091: E/AndroidRuntime(23277): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@40ece840 is not valid; is your activity running?
06-20 09:15:47.091: E/AndroidRuntime(23277): at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
06-20 09:15:47.091: E/AndroidRuntime(23277): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
06-20 09:15:47.091: E/AndroidRuntime(23277): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
06-20 09:15:47.091: E/AndroidRuntime(23277): at android.widget.PopupWindow.invokePopup(PopupWindow.java:993)
06-20 09:15:47.091: E/AndroidRuntime(23277): at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:899)
06-20 09:15:47.091: E/AndroidRuntime(23277): at android.widget.ListPopupWindow.show(ListPopupWindow.java:603)
解决方案
Better to use dialog(android.app.Dialog) to implement AutoCompleteTextView.In my opinion its not possible to add AutoCompleteTextView in PopupWindow(you will get Exception).You can add Spinner in Popupwindow.You can implement both if are using dialog instead of popup.
这篇关于自动完成的TextView在PopupWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!