参考资料:
1.popupwindow中的Edittext长按不出现输入法选择框
http://www.eoeandroid.com/thread-93841-1-1.html
2. Edittext不能复制和粘贴
http://www.eoeandroid.com/thread-283339-1-1.html
3. EditText 长按弹出的上下文菜单
http://www.apkbus.com/android-32266-1-1.html
4. Android 之 下拉框(Spinner)的使用
http://imshare.iteye.com/blog/770950
5.Android的EditText长按只显示上面的复制粘贴菜单,不要弹出下面的输入法键盘怎么做?
6.长按EditText后弹出的编辑文本有关问题
http://www.myexception.cn/android/347197.html
7. android UI进阶之弹窗的使用(2)--实现通讯录的弹窗效果
http://blog.csdn.net/notice520/article/details/6691246
8. Spinner 取消默认选中
http://jiangsy1107.blog.sohu.com/174405573.html
9. Spinner如何取消默认选择
http://www.eoeandroid.com/thread-40548-1-1.html
10. Android 动态添加/删除的Spinner菜单
http://android.tgbus.com/Android/tutorial/201105/352290.shtml
11. How to hide one item in an Android Spinner
http://stackoverflow.com/questions/9863378/how-to-hide-one-item-in-an-android-spinner
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
将语句1改为语句2即可!如下
语句1:context = getActivity().getApplicationContext();
语句2:context = getActivity();
参考链接:
http://blog.csdn.net/wsz1z154/article/details/7724912
以下代码用于在PopupWindow中实现长按EditText弹出spinner列表
final String[] options={"全选","剪切","复制","粘帖"}; final Spinner spinner = (Spinner) view.findViewById(R.id.spinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item,options); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { ToastUtil.show("you select >> " + options[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); popWin.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { editText.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(View v) { Logger.v(TAG, "editText long click"); // InputMethodManager imm = (InputMethodManager) context // .getSystemService(Context.INPUT_METHOD_SERVICE); // imm.showInputMethodPicker(); spinner.performClick(); return false; } }); return false; } });
使用反射机制调用系统类的私有方法
try { Class<EditText> cls = EditText.class; Method setSoftInputShownOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class); setSoftInputShownOnFocus.setAccessible(true); setSoftInputShownOnFocus.invoke(editText, false); } catch (Exception e) { e.printStackTrace(); }
spinner弹出下拉列表后就不能在adapter中添加选项否则出现异常
1.java.lang.NoSuchMethodError: android.widget.ArrayAdapter.addAll
2.java.lang.UnsupportedOperationException
动态构造长按文本框弹出的选项
private List<String> getOptionList(EditText editText) { List<String> optionList = new ArrayList<String>(); boolean select = false; boolean cut = false; boolean copy = false; boolean paste = false; boolean inputmethod = true; ClipboardManager clipBoard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); int start = editText.getSelectionStart(); int end = editText.getSelectionEnd(); cut = copy = (start != end); paste = clipBoard.hasText(); select = (!editText.getText().toString().isEmpty()); optionList.add(""); if (select) { optionList.add("全选"); } if (cut) { optionList.add("剪切"); } if (copy) { optionList.add("复制"); } if (paste) { optionList.add("粘帖"); } if (inputmethod) { optionList.add("输入法"); } return optionList; } private String[] getOptions(EditText editText) { List<String> optionList = getOptionList(editText); String[] options = new String[optionList.size()]; for (int i = 0; i < options.length; i++) { options[i] = optionList.get(i); } return options; }
自定义ArrayAdapter隐藏某个选项
public class CustomAdapter extends ArrayAdapter<String> { private int hidingItemIndex; public CustomAdapter(Context context, int textViewResourceId, String[] objects, int hidingItemIndex) { super(context, textViewResourceId, objects); this.hidingItemIndex = hidingItemIndex; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { View v = null; if (position == hidingItemIndex) { TextView tv = new TextView(getContext()); tv.setVisibility(View.GONE); v = tv; } else { v = super.getDropDownView(position, null, parent); } return v; } } private ArrayAdapter<String> getSpinnerAdapter(EditText editText) { String[] options = getOptions(editText); int hidingItemIndex = 0; CustomAdapter dataAdapter = new CustomAdapter(context, android.R.layout.simple_spinner_item, options, hidingItemIndex); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); return dataAdapter; }
长按文本框弹出spinner下拉列表
final EditText editText = (EditText) view.findViewById(R.id.comment_et); editText.setText(textView.getText()); final Spinner spinner = (Spinner) view.findViewById(R.id.spinner); spinner.setPrompt("编辑文字"); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String text = spinner.getAdapter().getItem(position).toString(); if(!text.equals("")) { ToastUtil.show(text); } } @Override public void onNothingSelected(AdapterView<?> parent) { } }); editText.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(View v) { Logger.v(TAG, "editText long click"); spinner.setAdapter(getSpinnerAdapter(editText)); spinner.performClick(); return false; } });
PopupWindow中的EditText不支持文字编辑操作:选择文字、全选、剪切、复制、粘帖。