2017-09-19 63 views
1

我正在尝试为Android学校项目制作蓝牙控制器。见图片:Xamarin.Forms的子类,按钮的事件处理程序

enter image description here

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> 

现在我需要的按钮2个事件处理程序,对于按下和释放时,时。这个想法就像在这篇文章中:Xamarin.Forms - Button Pressed & Released Event 但我不知道如何使Xamarin.Forms下的子类。

这里是我的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); 
     } 
    } 
} 

回答

0

修改的OnCreate来获取对按钮的引用。然后调用SetOnTouchListener为触摸事件提供一个处理程序:

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); 
} 

变化MainActivity.cs所以它实现View.IOnTouchListener,并补充说,通过接口所需的OnTouch方法。添加以下代码以重新定位按钮,以响应在屏幕上移动的触摸:

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; 
    } 
}