按下ImageButton时更改图像

时间:2012-10-01 23:36:31

标签: xamarin.android imagebutton

我的Android应用程序中有一个ImageButton,我希望应用程序在按下按钮时更改图像,当用户释放按钮时,它将返回默认图像。

P.S。我正在使用mono for android而且我必须以编程方式执行此操作,我不能不幸地使用XML标记。

1 个答案:

答案 0 :(得分:3)

您可以浏览视图的Touch事件来执行此操作:

var button = FindViewById<ImageButton>(Resource.Id.MyImageButton);

button.Touch += (object sender, View.TouchEventArgs e) => {
    if (e.Event.Action == MotionEventActions.Down) {
        button.SetImageResource(Resource.Drawable.Icon);
    } else if (e.Event.Action == MotionEventActions.Up) {
        button.SetImageResource(Android.Resource.Drawable.IcMenuGallery);
    }
};