我正在尝试为学校项目制作适用于Android的蓝牙控制器。见图:
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 :(得分: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;
}
}