Reputation: 3245
In a Xamarin.Android app, following the example at https://developer.xamarin.com/videos/?v=Activities_and_Intents (19:55) I am trying to assign an event handler to the onclick event of a button, and smilarly to the video above, I'd like not to call a delegate or lambda but rather just call a method defined elsewhere. This is not working in my code, but if I use a delegate it does... Can you explain why?
Also, I am trying to do the same with an EditText, for which I'd like to add event for when text is changed. But this is not working too..
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = "clicked!" };
// this works
Button searchButton = FindViewById<Button>(Resource.Id.searchButton);
searchButton.Click += SearchButton_Click;
EditText artistNameEditText = FindViewById<EditText>(Resource.Id.artistNameEditText);
artistNameEditText.TextChanged += artistNameEditText_TextChanged;
}
private void SearchButton_Click(object sender, EventArgs e)
{
Console.WriteLine("button pressed!");
// this is not working!
}
private void artistNameEditText_TextChanged(object sender, TextChangedEventArgs e)
{
Console.WriteLine("text has changed!");
// this is not working!
}
}
Upvotes: 2
Views: 10713
Reputation: 3245
There is nothing wrong with my code.
The reason it was not working is... I was not uninstalling my app from the device before deploying the new version to it, so I was running the old version over and over again. I took for granted that the old version would be replaced by the new one, just like when you install a new version of the same APK again. It was a bit problematic to spot because I was not making any changes to the UI, but only experimenting with event handlers.
Well, lesson learned, hope this helps someone else...
Upvotes: 4
Reputation: 1763
the problem is that you are using Console in android app, just look to the code below and adjast your code and make sure to remove Console statements (use something else such as Toast).
private void SearchButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Text = "Search Button Clicked!";
// Now it is working! :)
}
private void artistNameEditText_TextChanged(object sender, TextChangedEventArgs e)
{
Toast.MakeText(this, "Text has just changed!", ToastLength.Long).Show();
// Now it is working! :)
}
Upvotes: 0
Reputation: 591
This is an easy one first implement Android.Text.ITextWatcher in your MainActivity like this:
public class MainActivity : Activity, Android.Text.ITextWatcher
{
public void AfterTextChanged (Android.Text.IEditable s)
{
}
public void BeforeTextChanged (Java.Lang.ICharSequence s, int start, int count, int after)
{
}
public void OnTextChanged (Java.Lang.ICharSequence s, int start, int before, int count)
{
//were text is changed
}
...
}
and suscribe the TextChangedListener to your element using this
EditText artistNameEditText = FindViewById<EditText>(Resource.Id.artistNameEditText);
artistNameEditText.AddTextChangedListener (this);
Upvotes: 1
Reputation: 7392
Its my much simpler to use the Xamarin version of it.
textfield.TextChanged += (sender, e) =>
{
var abc = 123;
};
Hope that works for you :)
Upvotes: 0