I have set an event listener for two buttons and event listener for a EditText control in my java code:
public void doNewButtonClick( View view ) {
View.OnClickListener onSnap = new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView calculatorTextView = (TextView) findViewById(R.id.textView);
calculatorTextView.setText( "GO TO SLEEP" );
Log.d(TAG, "In the Handler for NEW BUTTONS");
}
};
Button button1 = (Button) findViewById(R.id.buttonA);
Button button2 = (Button) findViewById(R.id.buttonB);
button1.setOnClickListener( onSnap );
button2.setOnClickListener( onSnap );
}
public void onEditTextInput( View view ){
EditText myMessage = (EditText) findViewById(R.id.userText);
myMessage.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionID, KeyEvent event) {
Log.d(TAG, "BEFORE THE SEND");
EditText userMessage = (EditText) textView;
TextView phone = (TextView)findViewById(R.id.textView2);
if( actionID == EditorInfo.IME_ACTION_GO){
Log.d(TAG, "IN THE SEND");
phone.setText( userMessage.getText() );
}
return false;
}
});
}
This is my xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/buttonA"
android:layout_below="@+id/button6"
android:layout_toLeftOf="@+id/button6"
android:layout_toStartOf="@+id/button6"
android:layout_marginTop="119dp"
android:drawableLeft="@drawable/abc_ic_star_black_16dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/buttonB"
android:layout_alignBottom="@+id/buttonA"
android:layout_alignRight="@+id/button6"
android:layout_alignEnd="@+id/button6" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userText"
android:layout_above="@+id/buttonB"
android:layout_alignLeft="@+id/buttonA"
android:layout_alignStart="@+id/buttonA"
android:imeOptions="actionGo"
android:inputType="text"
android:layout_alignRight="@+id/buttonB"
android:layout_alignEnd="@+id/buttonB" />
I have two issues: 1) The event handlers don't work unless I also go into the control's properties and set the OnClick property to the desired method. In other words, I'll click a control many times and nothing will happen unless I set the OnClick property to a function, then the controls and their event handlers work as expected.
2) Setting the OnClick property of each control to the desired event handler( callback function) forces me to have to click the control twice for it to work. This happens since I am setting the OnClick property and also defining the OnClick function in the event handler.
Why are my event handlers not responding? Why do I have to set the OnClick property? Am I forced to set the OnClick property and not use the Anonymous class method to make event handlers?
Thank you!!