请问在扩展 extends view中如何在引用其的程序中触发事件?
本帖最后由 itakeblue 于 2013-12-13 10:13:47 编辑 我有一个扩展的edittext,如下:
test.class
public class test extends EditText {
public test(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event)
{
if (event.getKeyCode() == android.view.KeyEvent.KEYCODE_BACK){
//to do something
}
return super.onKeyPreIme(keyCode, event);
}
}
private test editText1;
@Override
public void onCreate(Bundle savedInstanceState) {
editText1 = (test) findViewById(R.id.editText1);
}
public void back_pressed(){
//some code
}
public class test extends EditText {
public test(Context context, AttributeSet attrs) {
super(context, attrs);
}
OnKeyBackPressed keybackpressed = null;
public void SetOnKeybackPressed(OnKeyBackPressed k) {
keybackpressed = k;
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == android.view.KeyEvent.KEYCODE_BACK) {
// to do something
if (keybackpressed != null)
keybackpressed.back_pressed();
}
return super.onKeyPreIme(keyCode, event);
}
public static interface OnKeyBackPressed {
public void back_pressed();
}
}
editText1.SetOnKeybackPressed(new OnKeyBackPressed() {
@Override
public void back_pressed() {
// ToDo something
}
});