Java Event listening compared to AS3
ok first of all sorry if its a stupid question, but I have ADHD and my head is going to explode after reading a lot of useless details, I just want a small, clear example and I'll predict the rest!
Basically I am playing with the Android SDK, and thought its similar to Adobe Flex, and everything went smooth until I tried listening to a Button click.
in AS3:
private function onAddedToStage(event:Event):void
{
myButton.addEventListener(MouseEvent.CLICK,handleClick);
}
private function handleClick(event:MouseEvent):void
{
trace("You clicked: " + event.currentTarget.name);
}
what I've done so far with trial and error:
private Button _okButton;
private EditText _name;
private View _view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("trace", "it works!");
_view = getCurrentFocus();
_name = (EditText) _view.findViewById(R.id.editText1);
_okButton = (Button) _view.findViewById(R.id.button1);
_name.setText("Yupee I found it!");
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.开发者_C百科d("trace", "Button clicked");
}
};
_okButton.setOnClickListener(l);
}
The code above crashes the application
Thanks in advance.
replace _view by this.
精彩评论