2017-09-19 3 views
1

J'essaie de faire un contrôleur Bluetooth pour Android pour un projet d'école. Voir image:Sous-classe pour Xamarin.Forms, gestionnaire d'événements pour les boutons

enter image description here

Code AXML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:text="0" 
    android:textSize="70sp" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:layout_above="@+id/bar" 
    android:gravity="center" 
    android:id="@+id/text" /> 
<ProgressBar 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:layout_above="@+id/button_forward" 
    android:rotation="90" 
    android:progress="100" 
    android:id="@+id/bar" /> 
<Button 
    android:text="Forward" 
    android:textSize="16dp" 
    android:layout_height="120dp" 
    android:layout_width="120dp" 
    android:layout_above="@+id/button_fire" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/button_forward" /> 
<Button 
    android:text="Fire" 
    android:textSize="16dp" 
    android:layout_height="120dp" 
    android:layout_width="120dp" 
    android:layout_above="@+id/button_backward" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/button_fire" /> 
<Button 
    android:text="Backward" 
    android:textSize="16dp" 
    android:layout_height="120dp" 
    android:layout_width="120dp" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/button_backward" /> 
<Button 
    android:text="Left" 
    android:textSize="16dp" 
    android:layout_height="120dp" 
    android:layout_width="120dp" 
    android:layout_above="@+id/button_backward" 
    android:layout_toLeftOf="@+id/button_fire" 
    android:id="@+id/button_left" /> 
<Button 
    android:text="Right" 
    android:textSize="16dp" 
    android:layout_height="120dp" 
    android:layout_width="120dp" 
    android:layout_above="@+id/button_backward" 
    android:layout_toRightOf="@+id/button_fire" 
    android:id="@+id/button_right" /> 
</RelativeLayout> 

Maintenant, je dois obtenir 2 gestionnaires d'événements sur les boutons, pour lorsqu'il est pressé et lorsqu'il est relâché. L'idée serait comme dans ce post: Xamarin.Forms - Button Pressed & Released Event Mais je ne sais pas comment faire la sous-classe sous Xamarin.Forms.

Voici le code de mon MainActivity:

using System; 
using System.Collections; 
using Android.App; 
using Android.Widget; 
using Android.OS; 
using Android.Bluetooth; 
using Android.Content; 
using Android.Views; 

namespace Bluetooth_Controller 
{ 
    [Activity(Label = "Bluetooth_Controller", MainLauncher = true)] 
    public class MainActivity : Activity 
    { 
     BluetoothAdapter BTAdapter; 
     Button button_Forward, button_Fire, button_Backward, button_Left, button_Right; 

     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 

      // Set our view from the "main" layout resource 
      SetContentView(Resource.Layout.Main); 

      // Initialize Components 
      Initialize(); 

      // Get local Bluetooth Adapter 
      BTAdapter = BluetoothAdapter.DefaultAdapter; // Default adapter 
     } 

     public void Initialize() 
     { 
      button_Forward  = (Button)FindViewById(Resource.Id.button_forward); 
      button_Fire   = (Button)FindViewById(Resource.Id.button_fire); 
      button_Backward  = (Button)FindViewById(Resource.Id.button_backward); 
      button_Left   = (Button)FindViewById(Resource.Id.button_left); 
      button_Right  = (Button)FindViewById(Resource.Id.button_right); 
     } 
    } 
} 

Répondre

0

Modifier OnCreate pour obtenir une référence au bouton. Appelez ensuite SetOnTouchListener pour fournir un gestionnaire pour l'événement tactile:

protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 
    SetContentView(Resource.Layout.Main); 
    button_Forward = FindViewById<Button>(Resource.Id.button_forward); 
    button_Forward.SetOnTouchListener(this); 
} 

Changer MainActivity.cs afin qu'il implémente View.IOnTouchListener et ajouter la méthode OnTouch qui est requis par l'interface. Ajoutez le code suivant pour repositionner le bouton en réponse au toucher se déplaçant sur l'écran:

public class Activity1 : Activity, View.IOnTouchListener 
{ 
    public bool OnTouch(View v, MotionEvent e) 
    { 
     switch (e.Action) 
     { 
      case MotionEventActions.Down: 
       //Do whatever you want in Down Key 
       break; 
      case MotionEventActions.Up: 
       //Do whatever you want in Up Key 
       break; 
     } 
     return true; 
    } 
}