I have a customized EditText, which need to do customized "paste".
I overrode onTextContextMenuItem(int id) to handle "paste" requested by selecting context menu.
@Override
public boolean onTextContextMenuItem(int id) {
switch(id){
case android.R.id.paste:
doMyPaste();
return true;
}
}
This works in Android before 3.0.
In 3.0, however, there is a small "paste" widget near the cursor widget if it's long-pressed, or the cursor is tapped. When user do "paste" from this widget, theonTextContextMenuItem(int id)
won't be invoked. As a result, I can't do the customized paste.
Do any one knows what th开发者_如何学Pythonat small "paste" widget is? Which method should I overrode to do a my own "paste"? To cover all bases, this has to be API-specific, so you must commit to doing it two different ways anyway.
For new APIs, the new android.content.ClipboardManager
interface provides everything you need to transfer any MIME type you want.
For old APIs, you must be tricksy if you expect to play with the old android.text.ClipboardManager
. Just base-64 encode the data of your Image (or whatever) and send that as text. On the receiving side, just reverse the process.
You can even "auto-detect" by determining whether you have android.text.ClipboardManager
or android.content.ClipboardManager
and act accordingly!
Also, your handler method should be returning super.onTextContextMenuItem(id)
if you don't process anything. Maybe an editing artifact?
As far as the Paste Widget, that's not present in old APIs, or may be present on certain OEM UIs, and you are probably left with implementing that yourself, or using a degraded method of interaction. Once you put text on the clipboard, the Paste command shows up in the "normal" context menus.
call past event inside afterTextChanged() method check now
editBox.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// do with text
}
@Override
public void afterTextChanged(Editable s) {
// do with text
Toast.makeText(getApplicationContext(), "call past", Toast.LENGTH_SHORT).show();
}
});
精彩评论