Created
December 17, 2013 17:17
-
-
Save bolhoso/8008744 to your computer and use it in GitHub Desktop.
Um TExtView custom que intercepta eventos de paste
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.interceptedittext; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.widget.EditText; | |
/** | |
* A TextView that supports true type fonts directly as a property in the XML. | |
* | |
* @see CachedTrueTypeDelegate check for more details adding fonts | |
*/ | |
public class InterceptEditText extends EditText { | |
public InterceptEditText(Context context) { | |
super(context); | |
} | |
public InterceptEditText(Context context, AttributeSet attrs, int defStyle) { | |
super (context, attrs, defStyle); | |
} | |
public InterceptEditText(Context context, AttributeSet attrs) { | |
super (context, attrs); | |
} | |
@Override | |
public void setText(CharSequence text, BufferType type) { | |
super.setText(text, type); | |
} | |
/** | |
* <p>This is where the "magic" happens.</p> | |
* <p>The menu used to cut/copy/paste is a normal ContextMenu, which allows us to | |
* overwrite the consuming method and react on the different events.</p> | |
* @see <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/widget/TextView.java#TextView.onTextContextMenuItem%28int%29">Original Implementation</a> | |
*/ | |
@Override | |
public boolean onTextContextMenuItem(int id) { | |
// Do your thing: | |
boolean consumed = super.onTextContextMenuItem(id); | |
// React: | |
switch (id){ | |
case android.R.id.paste: | |
onTextPaste(); | |
break; | |
} | |
return consumed; | |
} | |
/** | |
* Text was pasted into the EditText. | |
*/ | |
public void onTextPaste(){ | |
this.setText("ahh:" +getText().toString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<com.example.interceptedittext.InterceptEditText | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/hello_world" /> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment