question

njsokalski avatar image
0 Votes"
njsokalski asked RobCaplan edited

Raise FocusChange Event of Containing GridLayout

I have a custom class that inherits from GridLayout. One of the elements contained in the xml layout file is an EditText, and I want to be able to handle the EditText's FocusChange event. I figured the best way to do this would be to bubble the EditText's FocusChange event up to the GridLayout's FocusChange event. However, I cannot figure out how to do this. Here is my current code:
public class NamesPlayer : GridLayout
{
public string Name { get; set; }
public string DefaultName { get; set; }

     public NamesPlayer(Context context) : base(context) { this.Initialize(context); }
     public NamesPlayer(Context context, IAttributeSet attrs) : base(context, attrs) { this.Initialize(context, attrs); }
     public NamesPlayer(Context context, IAttributeSet attrs, int defstyleattrs) : base(context, attrs, defstyleattrs) { this.Initialize(context, attrs); }
     public NamesPlayer(Context context, IAttributeSet attrs, int defstyleattrs, int defstyleres) : base(context, attrs, defstyleattrs, defstyleres) { this.Initialize(context, attrs); }
    
     void Initialize(Context context, IAttributeSet attrs = null)
     {
         Inflate(context, Resource.Layout.NamesPlayer, this);
         ImageButton btnPlayer = FindViewById<ImageButton>(Resource.Id.btnPlayer);
         EditText txtPlayer = FindViewById<EditText>(Resource.Id.txtPlayer);
    
         btnPlayer.Click += (object sender, System.EventArgs e) => { this.CallOnClick(); };
         //txtPlayer.FocusChange += this.txtPlayer_FocusChange;
    
         if (attrs != null)
         {
             TypedArray ta = context.ObtainStyledAttributes(attrs, Resource.Styleable.NamesPlayer);
             txtPlayer.Text = ta.GetString(Resource.Styleable.NamesPlayer_Name);
             txtPlayer.Hint = ta.GetString(Resource.Styleable.NamesPlayer_DefaultName);
             ta.Recycle();
         }
     }
    
     private void txtPlayer_FocusChange(object sender, FocusChangeEventArgs e) { }
 }
dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @njsokalski I created a basic demo to test your code, it works as expected on my side. The FocusChange event is triggered when the editText's focus status is changed. I commented the code of the ‘if’ sentence because don't have the ‘styleable’ code, it should not make a difference.

How did you add the custom control to the layout xml? Try to clean and rebuild the project. Then open Toolbox to get the control and drag it to the xml.

0 Votes 0 ·

I think you misunderstood what I am looking to be able to do. Notice that the handler for the Click event of btnPlayer calls this.CallOnClick() so that I can handle the Click event of the object I create as follows:

private NamesPlayer np = FindViewById<NamesPlayer>(Resource.Id.npPlayer1);
this.np.Click += (object sender, EventArgs e) => { };

I want to do the same thing for the FocusChange event of txtPlayer using code such as:

private NamesPlayer np = FindViewById<NamesPlayer>(Resource.Id.npPlayer1);
this.np.FocusChange += (object sender, FocusChangeEventArgs e) => { };

However, because there is no "CallOnFocusChange()" method, I cannot use a method like I did for the Click event. How can I raise the FocusChange event of my custom class?

0 Votes 0 ·

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

Hello,​

Welcome to our Microsoft Q&A platform!

How can I raise the FocusChange event of my custom class?

Sorry for the mistake. For this, try to create a custom Event in the 'NamesPlayer' class and invoke the event in the editText's FocusChange event. Then implement the custom event in the Activity class.

Check the code:

public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        var customLayout = FindViewById<NamesPlayer>(Resource.Id.namesPlayer1);

        customLayout.Click += CustomLayout_Click;
        customLayout.TestEvent += CustomLayout_TestEvent;
    }
}

public class NamesPlayer : GridLayout
{
    public event EventHandler TestEvent;
    ...
    void Initialize(Context context, IAttributeSet attrs = null)
    {
        Inflate(context, Resource.Layout.NamesPlayer, this);
        EditText txtPlayer = FindViewById<EditText>(Resource.Id.edit);

        txtPlayer.FocusChange += this.txtPlayer_FocusChange;
    }

    private void txtPlayer_FocusChange(object sender, FocusChangeEventArgs e)
    {
        TestEvent.Invoke(sender, e);
    }
}


Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.